Poor Contrast Image?

2 views (last 30 days)
Sachin
Sachin on 21 Jun 2012
I used a threshold technique to change the black backround of an RGB image to Gray with a but it didn't work properly. Since it changes the pixels which are black inside the image also to gray. The code used is:
X=imread('f100.bmp);
im2=x;
greycol= intmax(class(X))/2;
[rows,cols,panes] = size(X);
blacks = find(~sum(X,3));
im2( [blacks,blacks + rows*cols,blacks+2*rows*cols] ) = greycol;
imshow(im2)
Can anybody help me in improving the code or any suggestions?

Answers (2)

Image Analyst
Image Analyst on 22 Jun 2012
Try this code:
clc;
clearvars;
close all;
workspace;
fontSize = 20;
% Read in color demo image.
folder = 'C:\Users\Sachin\Documents\Temporary';
baseFileName = 't56hll.jpg';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Binarize the red channel low enough to leave unwanted clutter.
binaryImage = redChannel > 30;
% Display the binary image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Initial Binary Image', 'FontSize', fontSize);
% Measure areas
labeledImage = bwlabel(binaryImage);
blobMeasurements = regionprops(binaryImage, 'area');
% Now I'll demonstrate how to select certain blobs based using the ismember function.
allBlobAreas = [blobMeasurements.Area];
% Get a list of the blobs that meet our criteria and we need to keep.
allowableAreaIndex = allBlobAreas == max(allBlobAreas); % Take the largest object only.
keeperIndex = find(allowableAreaIndex);
% Extract only those blobs that meet our criteria, and
% eliminate those blobs that don't meet our criteria.
% Note how we use ismember() to do this.
keeperBlobsImage = ismember(labeledImage, keeperIndex);
% Fill in holes
mask = imfill(keeperBlobsImage, 'holes');
% Display the binary image.
subplot(2, 2, 3);
imshow(mask, []);
title('Final Mask Image', 'FontSize', fontSize);
% Mask the image.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask,class(rgbImage)));
subplot(2,2,4);
imshow(maskedRgbImage);
title('Final Masked RGB Image', 'FontSize', fontSize);
  2 Comments
Sachin
Sachin on 22 Jun 2012
HI There, This works fantastic and I understand what you did to Initial Binary Image by filling up with holes. But the point was to get the gray background image as final result. I tried to manupulate your final mask but didn't get success, I tried following things as an extension of your code:
Grayimage=rgb2gray(rgbImage);
grayMaskedimage(~binaryImage)=150;
Mask2=zeros(480,480);
for i = 1:480
for j = 1:480
if grayMaskedimage(i,j) == 150
Mask2(i,j) = 1;
else
Mask2(i,j) = 0;
end
end
end
But this Mask2 doesn't seem alright.
Image Analyst
Image Analyst on 22 Jun 2012
So just add this line after the imfill() line:
% Invert it to get the gray background
mask = ~mask;
Plus, you've go to get away from that "for loop way of thinking" - you're not utilizing one of the best and most powerful features of MATLAB!

Sign in to comment.


Image Analyst
Image Analyst on 21 Jun 2012
Using any kind of point process like intlut() or im2(im2==0) = greycol (there - there's your suggestions) will change all black pixels to gray no matter where they are. If you have a foreground and background and your foreground object has some "holes" in it, then you need to create a mask that has foreground and background where the foreground is solid - no holes. Very easy to do. Why don't you upload an image somewhere, such as tinypic.com?
  1 Comment
Sachin
Sachin on 21 Jun 2012
Please follow the link below:
Original Image:
http://i48.tinypic.com/t56hll.jpg
Threholdded Image
http://i48.tinypic.com/34jf329.jpg

Sign in to comment.

Categories

Find more on Image Processing Toolbox 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!