How do I select and find the colours on the areas that edge detection has separated?

2 views (last 30 days)
My theory is that I can use edge detection in order to find objects within an image and then automatically paint those areas with their dominant colour. The first scale of my question is, how do I make the system choose the selected areas as different objects and then how do I colour them? The following is my code so far:
%read the picture
I = imread('bath.jpeg');
figure, imshow(I);
%turn the image in grayscale
%for two dimensional covertion
%to apply edge detection
J = rgb2gray(I);
threshold = graythresh(J);
figure, imshow(J);
%apply edge detection
ED = edge(J,'canny');
figure, imshow(ED);
OV=I;
OV1=I(:,:,1);
OV2=I(:,:,2);
OV3=I(:,:,3);
OV1(ED)=0;
OV2(ED)=255;
OV3(ED)=0;
OV(:,:,1)=OV1;
OV(:,:,2)=OV2;
OV(:,:,3)=OV3;
figure, imshow(OV);
So far the code is giving an image with a green edge detection overlay.

Accepted Answer

Image Analyst
Image Analyst on 20 Apr 2014
How are you going to do that? edges aren't always closed contours. Many are just simple straight lines or curves with 2 endpoints.
Maybe you'd rather use an approach like this:
or even the mean shift algorithm or k-means.
  5 Comments
Image Analyst
Image Analyst on 21 Apr 2014
You can do that but it may not give you what you expect because many or most of the regions are not totally enclosed. First make your enclosed regions solid with imfill
binaryImage = imfill(edgeImage, 'holes');
Then get your different color channels
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
Then measure the intensity of each within the white blobs
[labeledImage, numberOfRegions] = bwlabel(binaryImage);
redMeasurements = regionprops(labeledImage, redChannel, 'MeanIntensity', 'PixelIdxList');
greenMeasurements = regionprops(labeledImage, greenChannel, 'MeanIntensity', 'PixelIdxList');
blueMeasurements = regionprops(labeledImage, blueChannel, 'MeanIntensity', 'PixelIdxList');
% Now assign each region in the color channel to the mean color
for p = 1 : numberOfRegions
thisBlobsPixels = redMeasurement(p).PixelIdxList;
redChannel(thisBlobsPixels) = redMeasurements(p).MeanIntensity;
greenChannel(thisBlobsPixels) = greenMeasurements(p).MeanIntensity;
blueChannel(thisBlobsPixels) = blueMeasurements(p).MeanIntensity;
end
newRGBImage = cat(3, redChannel, greenChannel, blueChannel);
imshow(newRGBImage);
Note: this is untested, just off the top of my head so no gaurantees that it works as-is - it might need some tweaking but I hope you understand the concept of getting each blob's mean color and assigning it back to the blob in the image.
Leandros
Leandros on 22 Apr 2014
The code is working, but for only certain images. As I said before I don't need it to work perfectly, so I don't really mind that. Here is the result.
Thank you again for your time.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!