Я пытаюсь понять, как рисовать прямоугольники на изображении в Matlab.Рисование прямоугольников на изображении в Matlab
Как только прямоугольники нарисованы на изображении, я хотел бы сохранить изменения.
Заранее благодарен!
Я пытаюсь понять, как рисовать прямоугольники на изображении в Matlab.Рисование прямоугольников на изображении в Matlab
Как только прямоугольники нарисованы на изображении, я хотел бы сохранить изменения.
Заранее благодарен!
Использование 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]
и задается в единицах пикселей.
Я использую 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
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')
Без использования 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 :)