Image processing help. Multiplication of Images gives different results at Unit16, unit 32, unit 8.
2 views (last 30 days)
Show older comments
[EDIT: 20110611 23:32 CDT - reformat - WDR]
Codes :
a=imread('b.jpg');b=imread('c.jpg');c=a.*b;imshow(c)
a1=uint16(a);b1=uint16(b);c1=a1.*b1;imshow(c1)
a2=uint32(a);b2=uint32(b);c2=a2.*b2;imshow(c2)
I am not sure how to upload the picture I am using. I will let you know the problem now.
I am trying to 2 images. a and b.
1) Using unit 8 , I get some distortions.
2) Using unit 16, I am getting the required image.
3) Using unit 32, I am getting a complete black image.
Please let me know the problem. My friend needs this for her project. She asked me, I am not sure what is the problem. Please reply. Thank you.
0 Comments
Accepted Answer
Walter Roberson
on 12 Jun 2011
Check
class(a)
class(b)
I suspect you will find that your images are uint16, but uint8 could be possible as well.
To understand the distortion you get with uint8, take a look at this result:
>> uint8(2)*uint8(128)
ans =
255
To understand about getting the black image, read this image documentation in particular the section "True Color Images"
3 Comments
Walter Roberson
on 12 Jun 2011
The documentation I pointed to indicates:
===
The uint8 function maps any values in array that are outside the limit to the nearest endpoint. For example,
uint8(2^8) % 2^8 = 256
returns
ans =
255
===
This example uses exponentiation but the same thing happens with multiplication. Unless your values are restricted to 15 or less in the original image, when you multiply two of them you might end up with a value that is at least 16*16=256 which is "outside the limit" and will be mapped to "the nearest endpoint" (i.e., 255)
For the black image, refer to the second bit of documentation I indicated:
===
If you want to convert a true color image from one data type to the other, you must rescale the data. For example, this statement converts a uint8 true color image to double.
RGB64 = double(RGB8)/255;
or for uint16 images,
RGB64 = double(RGB16)/65535;
This statement converts a double true color image to uint8:
RGB8 = uint8(round(RGB64*255));
or to obtain uint16 images, type
RGB16 = uint16(round(RGB64*65535));
More Answers (1)
Alex Taylor
on 13 Jun 2011
Hi Vijay,
Walter's answer is accurate. I just wanted to add that since you have the Image Processing Toolbox, you might look at the Image Processing functions im2uint16 and im2uint32.
Unlike the MATLAB casting functions Walter used, the Image Processing Toolbox conversion functions manage both the casting and rescaling of your data when moving to other datatypes.
doc im2uint16
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!