How to label a pixel of color image in region growing ?

2 次查看(过去 30 天)
I have two image one of which is RGB color image and other is binary image containing seed pixels. I want to label(color) neighboring pixel by the seed pixel label(color) , if the euclidean distance between seed pixel and neighboring pixel less than by Threshold value (T) else by white color. Temp is used to store label(color) value of seed pixel....
clear all
img=imread('ldy.jpg');
figure, imshow(img), title('Input Image');
imggray=rgb2gray(img);
bin=imread('seedbinary.jpg');
figure, imshow(bin);
T = graythresh(imggray);
[rows, columns, numberOfColorBands] = size(img);
temp=zeros(rows, columns, numberOfColorBands);
for i=1:rows;
for j=1:columns;
if bin(i, j)==1;
temp(i, j, 1)=img(i, j, 1);
temp(i, j, 2)=img(i, j, 2);
temp(i, j, 3)=img(i, j, 3);
end
end
end
figure, imshow(temp), title('Seed Pixel Label');
for i=2:rows-1;
for j=2:columns-1;
if bin(i, j)==1;
d1=sqrt(double((img(i, j, 1)-img(i, j+1, 1))^2+(img(i, j, 2)-img(i, j+1, 2))^2+(img(i, j, 3)-img(i, j+1, 3))^2));
if d1<=T
img(i, j+1, 1)=temp(i, j, 1);
img(i, j+1, 2)=temp(i, j, 2);
img(i, j+1, 3)=temp(i, j, 3);
else
img(i, j+1, 1)=255;
img(i, j+1, 2)=255;
img(i, j+1, 3)=255;
end
end
end
end
figure, imshow(img), title('Output Image');
There is no error generated but not providing desired result.... Your effort will be appreciated ... Tell me where I am doing mistake....

回答(2 个)

David Young
David Young 2014-2-15
I suspect that the value of T is one problem. T is based on the image gray-levels, but it is used to threshold squared gray-level differences. T is probably not a good threshold for differences.
I suggest removing the call to graythresh, and simply setting T to a constant instead. Experiment with the value of the constant until you get a result that looks reasonable. After that worry about how to set T automatically if you need to.
If that doesn't help, please explain what is actually wrong with the results - in what way are they not what you expect?
  4 个评论
Rajesh  Gothwal
Rajesh Gothwal 2014-2-16
This is the code actually i am using. There are two problem with output image 1) Regions are not growing after one iteration and 2)Regions are not labeled by seed pixel color. No, I do not want same color. I want to color each region by the color of that region's seed pixel.
clear all
img=imread('ldy.jpg');
figure, imshow(img), title('Input Image');
imggray=rgb2gray(img);
bin=imread('seedbinary.jpg');
figure, imshow(bin), title('Seed Pixel in Binary');
T = graythresh(imggray);
[rows, columns, numberOfColorBands] = size(img);
temp=zeros(rows, columns, numberOfColorBands);
for i=1:rows;
for j=1:columns;
if bin(i, j)==1
temp(i, j, 1)=img(i, j, 1);
temp(i, j, 2)=img(i, j, 2);
temp(i, j, 3)=img(i, j, 3);
end
end
end
figure, imshow(temp), title('Seed Pixel Label');
for k=1:1000;
for i=2:rows-1;
for j=2:columns-1;
if bin(i, j)==1;
d1=sqrt(double((img(i, j, 1)-img(i, j+1, 1))^2+(img(i, j, 2)-img(i, j+1, 2))^2+(img(i, j, 3)-img(i, j+1, 3))^2));
if d1<=T
img(i, j+1, 1)=temp(i, j, 1);
img(i, j+1, 2)=temp(i, j, 2);
img(i, j+1, 3)=temp(i, j, 3);
else
img(i, j+1, 1)=255;
img(i, j+1, 2)=255;
img(i, j+1, 3)=255;
end
d2=sqrt(double((img(i, j, 1)-img(i-1, j+1, 1))^2+(img(i, j, 2)-img(i-1, j+1, 2))^2+(img(i, j, 3)-img(i-1, j+1, 3))^2));
if d2<=T
img(i-1, j+1, 1)=temp(i, j, 1);
img(i-1, j+1, 2)=temp(i, j, 2);
img(i-1, j+1, 3)=temp(i, j, 3);
else
img(i-1, j+1, 1)=255;
img(i-1, j+1, 2)=255;
img(i-1, j+1, 3)=255;
end
d3=sqrt(double((img(i, j, 1)-img(i-1, j, 1))^2+(img(i, j, 2)-img(i-1, j, 2))^2+(img(i, j, 3)-img(i-1, j, 3))^2));
if d3<=T
img(i-1, j, 1)=temp(i, j, 1);
img(i-1, j, 2)=temp(i, j, 2);
img(i-1, j, 3)=temp(i, j, 3);
else
img(i-1, j, 1)=255;
img(i-1, j, 2)=255;
img(i-1, j, 3)=255;
end
d4=sqrt(double((img(i, j, 1)-img(i-1, j-1, 1))^2+(img(i, j, 2)-img(i-1, j-1, 2))^2+(img(i, j, 3)-img(i-1, j-1, 3))^2));
if d4<=T
img(i-1, j-1, 1)=temp(i, j, 1);
img(i-1, j-1, 2)=temp(i, j, 2);
img(i-1, j-1, 3)=temp(i, j, 3);
else
img(i-1, j-1, 1)=255;
img(i-1, j-1, 2)=255;
img(i-1, j-1, 3)=255;
end
d5=sqrt(double((img(i, j, 1)-img(i, j-1, 1))^2+(img(i, j, 2)-img(i, j-1, 2))^2+(img(i, j, 3)-img(i, j-1, 3))^2));
if d5<=T
img(i, j-1, 1)=temp(i, j, 1);
img(i, j-1, 2)=temp(i, j, 2);
img(i, j-1, 3)=temp(i, j, 3);
else
img(i, j-1, 1)=255;
img(i, j-1, 2)=255;
img(i, j-1, 3)=255;
end
d6=sqrt(double((img(i, j, 1)-img(i+1, j-1, 1))^2+(img(i, j, 2)-img(i+1, j-1, 2))^2+(img(i, j, 3)-img(i+1, j-1, 3))^2));
if d6<=T
img(i+1, j-1, 1)=temp(i, j, 1);
img(i+1, j-1, 2)=temp(i, j, 2);
img(i+1, j-1, 3)=temp(i, j, 3);
else
img(i+1, j-1, 1)=255;
img(i+1, j-1, 2)=255;
img(i+1, j-1, 3)=255;
end
d7=sqrt(double((img(i, j, 1)-img(i+1, j, 1))^2+(img(i, j, 2)-img(i+1, j, 2))^2+(img(i, j, 3)-img(i+1, j, 3))^2));
if d7<=T
img(i+1, j, 1)=temp(i, j, 1);
img(i+1, j, 2)=temp(i, j, 2);
img(i+1, j, 3)=temp(i, j, 3);
else
img(i+1, j, 1)=255;
img(i+1, j, 2)=255;
img(i+1, j, 3)=255;
end
d8=sqrt(double((img(i, j, 1)-img(i+1, j+1, 1))^2+(img(i, j, 2)-img(i+1, j+1, 2))+(img(i, j, 3)-img(i+1, j+1, 3))^2));
if d8<=T
img(i+1, j+1, 1)=temp(i, j, 1);
img(i+1, j+1, 2)=temp(i, j, 2);
img(i+1, j+1, 3)=temp(i, j, 3);
else
img(i+1, j+1, 1)=255;
img(i+1, j+1, 2)=255;
img(i+1, j+1, 3)=255;
end
end
end
end
end
figure, imshow(img), title('Output Image');
% imwrite(img,'output.jpg','jpg')
Image Analyst
Image Analyst 2014-2-16
I didn't go over all of it but one thing that struck me immediately was clipping performed by this:
double((img(i, j, 1)-img(i, j+1, 1)
You can't subtract and then make it double. After subtraction the clipping has already happened so casting to double does not help at all. You need to cast each to double first, before the subtraction.
double((img(i, j, 1)) - double(img(i, j+1, 1))

请先登录,再进行评论。


Image Analyst
Image Analyst 2014-2-15
Please post your images. This does not seem like a particularly intelligent or robust algorithm but it might be okay for certain images. We can advise you better once we see your image(s).
  4 个评论
David Young
David Young 2014-2-16
As far as I can see, your code only ever modifies the image at the seed locations - the array bin is never modified, so each iteration makes changes at the same place. So there's no way that a value can propagate across a region.
If your problem is that the watershed oversegments, there's a demo that addresses it exactly. Look at this: http://www.mathworks.co.uk/products/demos/image/watershed/ipexwatershed.html - looks like just the thing, assuming you have the Image Processing Toolbox.
Image Analyst
Image Analyst 2014-2-16
That's one of my favorite demos. Rajesh's code reminded me a little bit of anisotropic diffusion the way he calculates the difference squared in the 8 directions. I've attached a demo of that, it's really nice.
But what I don't understand is why the code does an Otsu threshold on the grayscale version and then seems to only process within that region. And then when it does you get some hysteresis or something because when you're processing the (i,j) pixel, you make changes to a neighbor of that which is then checked as the 3x3 window moves away. I guess that is how it's propagating the region. Some comments would help.
Another problem is T. T is number between 0 and 1 while d1 through d8 are in the range 0-255 so the condition d1<=T will only ever be met for pixels that are exactly 0. I'm not really sure what the intent there is. So why are you setting the pixel to 0 (which is what temp is at those locations) when the pixel is already zero there? Again adding comments may help you talk this out with yourself to discover errors. Also, displaying intermediate images may help reveal what is going wrong.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by