If in a Image there are two black boxes and I have to find out that only those black blocks will be in image and the other part of the image become white. Then what should be the MATLAB coding for this?
Show older comments
Hi, I am working on image processing. In my image there are two black boxes attached with one hand. I have to keep only the two black boxes in that image and have to make the other part of the images as white. That means only the most black parts of the image will be remain in the frame. Then after finding this, I have to run the harris algorithm and find out the intensity of the pixel of those two black boxes. Now I convert the color image to the gray(black and white) image. But after that I become stuck in this condition.
Would anyone please help me on this?
Thanks in advance.
11 Comments
Image Analyst
on 29 Sep 2011
Where did you post your image?
Walter Roberson
on 29 Sep 2011
Please post a sample image.
http://www.mathworks.com/matlabcentral/answers/7924-where-can-i-upload-images-and-files-for-use-on-matlab-answers
Shamim Rahman
on 29 Sep 2011
Shamim Rahman
on 29 Sep 2011
Walter Roberson
on 30 Sep 2011
The numbers are interfering with the URLs, at least on my system. Copying, they are
http://www.sendspace.com/file/ct256i
http://www.sendspace.com/file/2iwurk
Walter Roberson
on 30 Sep 2011
The second of those is a MATLAB .fig file. It is easier if you post a JPG or TIF or the like. I do not have remote graphics access to MATLAB when I work from home.
Image Analyst
on 30 Sep 2011
If I don't see an image on the first click, I don't bother to proceed. He should have used something like tinypic.com.
Image Analyst
on 30 Sep 2011
Define "intensity". There would be different answers depending on how you got your gray image:
grayImage1 = rgb2gray(rgbImage);
grayImage2 = rgbImage(:,:,1);
grayImage3 = rgbImage(:,:,2);
grayImage4 = rgbImage(:,:,3);
Shamim Rahman
on 30 Sep 2011
Shamim Rahman
on 28 Oct 2011
Image Analyst
on 28 Oct 2011
Yeah, you're right - not very good. I guess you didn't understand what BlobsDemo was showing you or else you would have filtered out that other stuff and had only the squares left in there.
Accepted Answer
More Answers (2)
Image Analyst
on 29 Sep 2011
0 votes
Sounds like a straightforward adaption of BlobsDemo: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
Image Analyst
on 28 Oct 2011
Shamim: If you had followed my Blobs Demo, you would have ended up with this, which gives better results than your code above:
% Demo to find the squares
% By ImageAnalyst
%
clc; % Clear command window.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in a standard MATLAB color demo image.
folder = 'C:\Documents and Settings\username\My Documents\Downloads';
baseFileName = '2133oyq.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, 'Position', get(0,'Screensize'));
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);
% Threshold the red channel image
binaryImage = redChannel < 132;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Clean up. First remove stuff touching the border.
binaryImage2 = imclearborder(binaryImage);
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage2, []);
title('Binary Image 2', 'FontSize', fontSize);
% Still some debris. Find out how big it is.
labeledImage = bwlabel(binaryImage2);
measurements = regionprops(labeledImage, 'Area');
allBlobAreas = [measurements.Area]
% Hmmm ... Looks like squares are bigger than 700 pixels.
% Extract those out.
allowableAreaIndexes = allBlobAreas > 700; % Take the big objects.
keeperIndexes = find(allowableAreaIndexes);
% 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, keeperIndexes);
% Display the image.
subplot(2, 2, 4);
imshow(keeperBlobsImage, []);
title('Final Binary Image', 'FontSize', fontSize);
% Re-measure this final binary image to get the
% intensity of the squares.
% labeledImage = bwlabel(keeperBlobsImage);
measurements = regionprops(keeperBlobsImage, ...
redChannel, 'MeanIntensity');
allBlobIntensities = [measurements.MeanIntensity]
Results in command window:
allBlobAreas =
1 1 2 8 1 4 28 1 3 3 9 1 8 13 741 1 793
allBlobIntensities =
28.8367 31.7680
Walter's code works only for squares lined up with the rows and columns, not for images like yours. But you didn't specify or show us. That's why it's important for people to upload an image first BEFORE they post their question. It so much easier, and more useful to you, if you show us your image so we know what we're dealing with and don't guess and give you suggestions that won't be useful. It's harder to give answers on image processing without an image to work with, as I think you now realize.
2 Comments
Shamim Rahman
on 28 Oct 2011
Image Analyst
on 28 Oct 2011
That's a different image. So the 132 threshold might not work for that. If you're going to have all kinds of images with all kinds of bizarre, uneven illumination then you're going to need something more robust than simple fixed thresholding. And if I get it to work with both these images, then you'll come along with a third image it fails on. It's hard to make a robust algorithm if you don't have the full range of images to work with.
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!