【问题标题】:Draw rectangles on an image in Matlab在 Matlab 中的图像上绘制矩形
【发布时间】:2021-02-06 11:06:21
【问题描述】:

我想弄清楚如何在 Matlab 中的图像上绘制矩形。

在图像上绘制矩形后,我想保存更改。

提前致谢!

【问题讨论】:

    标签: matlab


    【解决方案1】:

    使用getframe

    img = imread('cameraman.tif');
    fh = figure;
    imshow( img, 'border', 'tight' ); %//show your image
    hold on;
    rectangle('Position', [50 70 30 60] ); %// draw rectangle on image
    frm = getframe( fh ); %// get the image+rectangle
    imwrite( frm.cdata, 'savedFileName.png' ); %// save to file
    

    有关绘制矩形的更多选项,请参阅rectanlge。矩形的'Position' 参数格式为[from_x from_y width height],并以像素为单位。

    【讨论】:

    • 图片太大不适合屏幕怎么办?有没有办法保持原始图像分辨率?
    • @ShmilTheCat 我想你可以设置'Position' 人物属性fh: set(fh, 'Position', [0 0 size(img,2), size(img,1)])?
    【解决方案2】:

    不使用getframe:

    im=imread('face.jpg'); %Image read
    rectangle('Position', [10 10 30 30] ,...
        'EdgeColor', 'r',...
        'LineWidth', 3,...
        'LineStyle','-');%rectangle properties
    imshow( im, rectangle); %draw rectangle on image.
    

    更多详情请访问this MathWorks thread :)

    【讨论】:

      【解决方案3】:

      最好和最简单的使用方法是在更新的 Matlab 版本中

      img = imread('abcd.jpg');
      box = [X1, Y1, width, height];    
      outImage= insertObjectAnnotation(img,'rectangle',bbox,'Rectangle');
      figure, imshow(outImage), title('Image with rectangle');
      

      【讨论】:

        【解决方案4】:

        我正在使用octave 并且函数getframe 不可用所以我写了这个微不足道的函数

        %% Draw red rectangle IN the image using the BoundingBox from regionprops
        function rgbI = drawRectangleOnImg (box,rgbI)
            x = box(2); y = box(1); w = box(4); h = box(3);
            rgbI(x:x+w,y,1)   = 255;
            rgbI(x:x+w,y+h,1) = 255;
            rgbI(x,y:y+h,1)   = 255;
            rgbI(x+w,y:y+h,1) = 255;
            rgbI(x:x+w,y,2)   = 0;
            rgbI(x:x+w,y+h,2) = 0;
            rgbI(x,y:y+h,2)   = 0;
            rgbI(x+w,y:y+h,2) = 0;
            rgbI(x:x+w,y,3)   = 0;
            rgbI(x:x+w,y+h,3) = 0;
            rgbI(x,y:y+h,3)   = 0;
            rgbI(x+w,y:y+h,3) = 0;
        end
        

        【讨论】:

          【解决方案5】:
           I=imread('%required image');   
          [M,N] = size(rgb2gray(I));%find the size of the image
          x=int16(M/3);y=int16(N/3);xwidth=int16(N/3); ywidth=int16(N/3);%specify the position
          pos=[x y xwidth ywidth];% give the position in which you wanna insert the rectangle
          imshow(I);hold on
          rectangle('Position',pos,'EdgeColor','b')
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-01-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-06-05
            • 1970-01-01
            • 1970-01-01
            • 2016-09-22
            相关资源
            最近更新 更多