convert the color of pictures
2 views (last 30 days)
Show older comments
Hi all, I'm a learner, need some help.
I have some emotion face pictures with color face on white background, format is bmp. I want to convert them to gray scale, that are gray face presented on gray background. Cause the pictures will be used as experiment material, i have to save them. But the question came up, i found when stored as bmp format, the pictures looks strange with distored color, then stored as jpg format, they looks fine. But when opened, I found the face presented on gray background, but under the gray background is bigger white background. Here is my code, I think there must be some serious problems. Please do me a favor, appreciate that.
clc;clear;
foldername=dir('E:\NimStim\')
for i=1:length(foldername)-2
filename=strcat('E:\NimStim\',foldername(i+2).name);
I=imread(filename);
if length(size(I))==3 %cause i found some picture is m*n*3,but not other pictures that are m*n
J=rgb2gray(I);
J(find(J>230))=205;
figure, imshow(J);
else
[X map]=imread(filename);X(find(X==0))=50;
gmap = rgb2gray(map);
figure, imshow(X,gmap);
end
saveas(figure(i),sprintf('figure_%d.bmp',i))
end
0 Comments
Accepted Answer
Image Analyst
on 25 Sep 2013
Never use jpg for image analysis or processing if you care about the accuracy of your data. BMP is also an old format with some problems (like huge file size). And it seems like they both want to save grayscale images as color unless you do something to prevent it. Use PNG format if you can, at least for the images that you create. It's a widely popular lossless compression format that's about a third the size of BMP files and there is no alteration of pixel values upon doing a round trip save/read of the data like there is with jpeg.
By the way, length(size(I)) is the same as ndim(I). Also, I is a horrible name for a variable. Looks too much like l and 1 in some fonts. And, since you have both lower and upper case i, a simple typo could lead to a serious bug. Plus this is not a very robust way of getting filenames that exist in a folder. I suggest you follow the code examples in the FAQ. figure(i) may also give you problems unless you have a "close all" before your loop because you may have a figure 1 already existing from another run of this, or other, code. See this link http://www.mathworks.com/matlabcentral/answers/8026-best-way-s-to-master-matlab
And why are you calling imread twice? Once is sufficient. You can use ind2rgb and then rgb2gray to convert your indexed images into a grayscale image (which you should call J since that's what you called it in the top block of the if statement.)
Finally, I don't know why you're saving the figure with background, axes, toolboars or whatever, rather than just saving the gray scale image itself with imwrite(). Can you explain why?
Hope this helps...
4 Comments
Image Analyst
on 26 Sep 2013
Put an object of known size into the image, say 10 cm, and measure how many pixels it is. Then multiply all distances in pixels by this calibration factor and areas by the calibration factor squared.
More Answers (0)
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!