首页 > 解决方案 > 如何在 MATLAB 中在我的原始图像上绘制线性回归线?

问题描述

我想在这张图片的边缘画一条红线,我想用线性回归来做,我已经得到了边缘的点,并通过图表上的点绘制了最好的线,但我不能t 找出如何使用这些信息在我的原始图像上绘制相同的线。

在此处输入图像描述


Image = imread('55-1_a.jpg');

imshow(Image)

binary_Image = imbinarize(Image);

[PIC_X, PIC_Y] = size(binary_Image);

imshow(binary_Image)



% i creat a matrix with two columns and PIC_X-1 rows  
black_pixel_on_edge = zeros(PIC_Y , 2); % in each row i will save the lowest black point to use it later in the linear regression





 for k = 1 : PIC_Y % i go through the columns of the image
     black_pixel_on_edge(k, 1) = k;

     for k2 = 1000 : 2000% i o through the rows
         if( binary_Image(k2, k) == 0)%if the pixel is black i should save it in my array(WIDTH)

             black_pixel_on_edge(k, 2) = k2;
         end

     end



 end


 %draw all the points from my Matrix
 scatter(black_pixel_on_edge(:,1),black_pixel_on_edge(:,2));
  axis equal tight;


 h1 = lsline;% draw a line with linear regression
 h1.Color = 'r';


%save the data of the red line
 p2 = polyfit(get(h1,'xdata'),get(h1,'ydata'),1);


 Slope_Of_Line = p2(1, 1);
 Hight_Of_Line = p2(1, 2);

 % here i just want to draw the same red line on my original 'Image'



标签: matlablinear-regression

解决方案


推荐阅读