Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
ROI of an image

Subject: ROI of an image

From: astro mmi

Date: 17 Sep, 2010 15:19:07

Message: 1 of 9

Hi everyone.,
    The following code reads an image, allows user to select an ROI. Then we plot 1000 points randomly from the ROI and generate a best fit line through the points. I see that the code works fine till the plot i.e, selection of ROI and randomly selected 1000 points are ok. However, i am skeptical about the plot and the best fit line. Pls help and let me know if I am making a mistake and where. Thanx in advance.

%This program generates the best fit line automatically and more accurately
%than thesisvertangle.m for extension angle

%April 18,2010

%program computes the extension angle w.r.t a normal automatically

clc;
close all;
clear all;

%WHEN USING THE IMAGE MAKE SURE THAT BW AND BW1 DO NOY HAVE TOO MANY ELEMENTS, IF THEY DO CROP BW AND BWI AND USE THE CROPPED IMAGES
%read bent image
q=imread('2.jpg');
figure,imshow(q)
%select roi
bw1=roipoly;
figure,imshow(bw1)

start_row = 256;
start_col = 190;
cropRGB = bw1(start_row:778, start_col:945, :);
figure,imshow(cropRGB)

%select 1000 pixels randomly from one of the vertebral regions
bw2 = cropRGB;
figure,imshow(bw2)

sizbw1=size(bw2);
%stores the x-coordinate of all the pixels with gray level 153
whitex=zeros(1,(sizbw1(1)*sizbw1(2)/2));
%stores the y-coordinate of all the pixels with gray level 153
whitey=zeros(1,(sizbw1(1)*sizbw1(2)/2));

e=1;
for c=1:sizbw1(1)
for d=1:sizbw1(2)
   if (bw2(c,d)==0)
   whitex(e)=d;
   whitey(e)=c;
   e=e+1;
   end
end
end
for ty=1:length(whitex)
    if(whitex(ty)~=0)
    whitexmod(ty)=whitex(ty);
    whiteymod(ty)=whitey(ty);
    end
end

needed=1000;
ind = randperm(length(whitexmod));
selected = ind(1:needed);
xselected = whitexmod(selected);
yselected = whiteymod(selected);
xcoord=xselected;
ycoord=yselected;
mydata=[xcoord;ycoord]; %first vertebra midpoint data plot
figure,plot(xcoord,ycoord,'o')
%best line fit
eq=polyfit(xcoord,ycoord,1);
%equation of line1
y = eq(1).* xcoord + eq(2);
figure,imshow(bw2)
axis on;
hold on;
plot(xcoord,y,'-')



 

Subject: ROI of an image

From: ImageAnalyst

Date: 17 Sep, 2010 18:41:22

Message: 2 of 9

Can you upload your image to http://drop.io so people can try your
code?

I didn't see anything off the top of my head but I couldn't run the
code because I didn't have the image and the sample image I tried
failed because you put some hard coded values in the imcrop.

Maybe you want to use polyval() to generate your y values.

Subject: ROI of an image

From: astro mmi

Date: 17 Sep, 2010 21:57:04

Message: 3 of 9

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <7036b573-c6f2-4464-bf58-873c76024d79@t3g2000vbb.googlegroups.com>...
> Can you upload your image to http://drop.io so people can try your
> code?
>
> I didn't see anything off the top of my head but I couldn't run the
> code because I didn't have the image and the sample image I tried
> failed because you put some hard coded values in the imcrop.
>
> Maybe you want to use polyval() to generate your y values.

Hi,
  I have added the image '2.jpg' on the website and this is the link:
http://drop.io/q827htt
Pls select the vertebral region as the ROI. Thanx.

Subject: ROI of an image

From: ImageAnalyst

Date: 17 Sep, 2010 23:34:30

Message: 4 of 9

That way was no good. Try it my way instead.

clc;
close all;
clear all;

%WHEN USING THE IMAGE MAKE SURE THAT BW AND BW1 DO NOY HAVE TOO MANY
ELEMENTS, IF THEY DO CROP BW AND BWI AND USE THE CROPPED IMAGES
%read bent image
q=imread('C:\Documents and Settings\tk2013\My Documents\Downloads
\2.JPG');
imshow(q)
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.
%select roi
uiwait(msgbox('Outline the spine. Right click when done.'));
bw1=roipolyold;
figure;
imshow(bw1)
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.

start_row = 256;
start_col = 190;
cropRGB = bw1(start_row:778, start_col:945, :);
figure;
imshow(cropRGB)
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.

%select 1000 pixels randomly from one of the vertebral regions
bw2 = cropRGB;
imshow(bw2)
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.

sizbw1=size(bw2)
[whiteymod whitexmod] = find(bw2);

needed=1000;
ind = randperm(length(whitexmod));
selected = ind(1:needed);
xselected = whitexmod(selected);
yselected = whiteymod(selected);
xcoord=xselected;
ycoord=yselected;
mydata=[xcoord;ycoord]; %first vertebra midpoint data plot
hold on;
plot(xcoord,ycoord,'o')
%best line fit
coefficients = polyfit(xcoord,ycoord,1);
%equation of line1
estimatedy = polyval(coefficients, xcoord);
axis on;
hold on;
plot(xcoord,estimatedy,'r-')

Subject: ROI of an image

From: astro mmi

Date: 29 Sep, 2010 14:53:06

Message: 5 of 9

Thanx for the help. I have posted the resulting image from your code at http://drop.io/pqdvqxw
I see that the selected points are all random and are not lying in the selected ROI and thus the line generated doesn't pass thro' the black region but is a best fit for the points scattered all across the figure. Pls help. I want the points to be selected only in the black region and a best fit line to be generated across it. Thanx in advance.



ImageAnalyst <imageanalyst@mailinator.com> wrote in message <744c0bdf-3f07-442f-894e-7a0bc491b76f@s19g2000vbr.googlegroups.com>...
> That way was no good. Try it my way instead.
>
> clc;
> close all;
> clear all;
>
> %WHEN USING THE IMAGE MAKE SURE THAT BW AND BW1 DO NOY HAVE TOO MANY
> ELEMENTS, IF THEY DO CROP BW AND BWI AND USE THE CROPPED IMAGES
> %read bent image
> q=imread('C:\Documents and Settings\tk2013\My Documents\Downloads
> \2.JPG');
> imshow(q)
> set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
> screen.
> %select roi
> uiwait(msgbox('Outline the spine. Right click when done.'));
> bw1=roipolyold;
> figure;
> imshow(bw1)
> set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
> screen.
>
> start_row = 256;
> start_col = 190;
> cropRGB = bw1(start_row:778, start_col:945, :);
> figure;
> imshow(cropRGB)
> set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
> screen.
>
> %select 1000 pixels randomly from one of the vertebral regions
> bw2 = cropRGB;
> imshow(bw2)
> set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
> screen.
>
> sizbw1=size(bw2)
> [whiteymod whitexmod] = find(bw2);
>
> needed=1000;
> ind = randperm(length(whitexmod));
> selected = ind(1:needed);
> xselected = whitexmod(selected);
> yselected = whiteymod(selected);
> xcoord=xselected;
> ycoord=yselected;
> mydata=[xcoord;ycoord]; %first vertebra midpoint data plot
> hold on;
> plot(xcoord,ycoord,'o')
> %best line fit
> coefficients = polyfit(xcoord,ycoord,1);
> %equation of line1
> estimatedy = polyval(coefficients, xcoord);
> axis on;
> hold on;
> plot(xcoord,estimatedy,'r-')

Subject: ROI of an image

From: astro mmi

Date: 29 Sep, 2010 15:21:05

Message: 6 of 9

This is the latest image upoaded at http://drop.io/w1tebqm#
I havemodified your code so that I may select only the black ROI. Now in the image that I have posted, I still see that the best fit line is not so accurate. Pls let me knw if it is right. I haven't made any other changes to your code that was last posted except for that you have selected the white region in your program for generating random points and I have selected the black region. Pls help and thanx in advance.





"astro mmi" <pyarsa_madhu@yahoo.co.in> wrote in message <i7vjsi$61g$1@fred.mathworks.com>...
> Thanx for the help. I have posted the resulting image from your code at http://drop.io/pqdvqxw
> I see that the selected points are all random and are not lying in the selected ROI and thus the line generated doesn't pass thro' the black region but is a best fit for the points scattered all across the figure. Pls help. I want the points to be selected only in the black region and a best fit line to be generated across it. Thanx in advance.
>
>
>
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <744c0bdf-3f07-442f-894e-7a0bc491b76f@s19g2000vbr.googlegroups.com>...
> > That way was no good. Try it my way instead.
> >
> > clc;
> > close all;
> > clear all;
> >
> > %WHEN USING THE IMAGE MAKE SURE THAT BW AND BW1 DO NOY HAVE TOO MANY
> > ELEMENTS, IF THEY DO CROP BW AND BWI AND USE THE CROPPED IMAGES
> > %read bent image
> > q=imread('C:\Documents and Settings\tk2013\My Documents\Downloads
> > \2.JPG');
> > imshow(q)
> > set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
> > screen.
> > %select roi
> > uiwait(msgbox('Outline the spine. Right click when done.'));
> > bw1=roipolyold;
> > figure;
> > imshow(bw1)
> > set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
> > screen.
> >
> > start_row = 256;
> > start_col = 190;
> > cropRGB = bw1(start_row:778, start_col:945, :);
> > figure;
> > imshow(cropRGB)
> > set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
> > screen.
> >
> > %select 1000 pixels randomly from one of the vertebral regions
> > bw2 = cropRGB;
> > imshow(bw2)
> > set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
> > screen.
> >
> > sizbw1=size(bw2)
> > [whiteymod whitexmod] = find(bw2);
> >
> > needed=1000;
> > ind = randperm(length(whitexmod));
> > selected = ind(1:needed);
> > xselected = whitexmod(selected);
> > yselected = whiteymod(selected);
> > xcoord=xselected;
> > ycoord=yselected;
> > mydata=[xcoord;ycoord]; %first vertebra midpoint data plot
> > hold on;
> > plot(xcoord,ycoord,'o')
> > %best line fit
> > coefficients = polyfit(xcoord,ycoord,1);
> > %equation of line1
> > estimatedy = polyval(coefficients, xcoord);
> > axis on;
> > hold on;
> > plot(xcoord,estimatedy,'r-')

Subject: ROI of an image

From: ImageAnalyst

Date: 29 Sep, 2010 15:41:07

Message: 7 of 9

I don't know. It's hard to see if the polyfit/polyval results are
accurate or not. I just assumed that they are. From your uploaded
pic, it looks like there are roughly the same number of points above
the line as below it, for any given x value.

If you don't like how they are performing then perhaps you can just
get the "orientation" measurement of regionprops(). Why are you
picking all these random coordinates anyway? Why not just get the
main axis through the thing (via regionprops)?

Subject: ROI of an image

From: astro mmi

Date: 29 Sep, 2010 19:17:26

Message: 8 of 9

If I use the major axis or minor axis in region props, it will just give me the length of the major and minor axes across the ROI. But, what I want is the plot of these axes against the ROI. Is there some way in which I can get that. Also, as you mentioned the line generated here is best fit as there are approximately equal number of points above and below the line. However, I want the line in such a way that there are equal number of points on either sdes of the line, so that is a vertical line. Thanx in advance.


ImageAnalyst <imageanalyst@mailinator.com> wrote in message <47c9176c-588d-4ffb-b0c7-07a1728b7392@j18g2000yqd.googlegroups.com>...
> I don't know. It's hard to see if the polyfit/polyval results are
> accurate or not. I just assumed that they are. From your uploaded
> pic, it looks like there are roughly the same number of points above
> the line as below it, for any given x value.
>
> If you don't like how they are performing then perhaps you can just
> get the "orientation" measurement of regionprops(). Why are you
> picking all these random coordinates anyway? Why not just get the
> main axis through the thing (via regionprops)?

Subject: ROI of an image

From: ImageAnalyst

Date: 29 Sep, 2010 19:29:58

Message: 9 of 9

The "orientation" measurement of regionprops(), along with the
centroid measurement of regionprops(), is an alternate way of getting
the info rather than using polyfit/polyval.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us