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?

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

Please post a sample image.
http://www.mathworks.com/matlabcentral/answers/7924-where-can-i-upload-images-and-files-for-use-on-matlab-answers
Dear Image Analyst,
I have sent you one msg about the location I uploaded my images.
Thank you
I have uploaded two files in the following locations:
1.http://www.sendspace.com/file/ct256i
2.http://www.sendspace.com/file/2iwurk
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
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.
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.
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);
In future I shall use tinypic.com. Thank you for your advise and help.
I worked on my questions. Thank you very much to Walter and Image Analyst. Finally I have found the solution of my questions. not very good but well. I have uploaded the input image and output image of my program.
Input Image: http://tinypic.com/r/2133oyq/5
Output Image: http://tinypic.com/r/xgfpua/5
Program: im1=ones([480,640]);
im2=imread('Image.jpg');
for x=1:250
for y=100:700
if (im2(x,y)<32)
im1(x,y)=0;
end
end
end
imwrite(im1,'clean.jpg');
figure,imshow('clean.jpg');
This one really works. So any one can try with it.
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.

Sign in to comment.

 Accepted Answer

imread() into a variable such as rgbimg . rgb2gray(rgbimg). Threshold the gray image like
bwimg = grayimage < your_threshold
bwlabel() the bw image. Then
[box1rows, box1cols] = find(labeledimage==1);
[box2rows, box2cols] = find(labeledimage==2);
And
rgbbox1r = rgbimg(box1rows,box1cols,1);
rgbbox1g = rgbimg(box1rows,box1cols,2);
rgbbox1b = rgbimg(box1rows,box1cols,3);
and the same for box2.
Now you can measure the intensity of the pixels by whatever means is appropriate for your purposes. (Though if all you are looking for is the luminosity, then extracting the original rgb is not needed and is inefficient compared to the alternatives.)

1 Comment

Thank you Walter for your help. I am trying with your coding. I shall inform you about this.

Sign in to comment.

More Answers (2)

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

Dear Image Analyst,
Thank you very much for your answer. I have used your codes for my image but it is detecting only one square block from the image not the other. I have uploaded the input image and the output result I got after running this program in the below links:
Input Image:
http://tinypic.com/r/x6ez4w/5
Output Image:
http://tinypic.com/r/16k3zw1/5
Would you please look at those again and advice me what should I do now?
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.

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!