Borrar filtros
Borrar filtros

how to creat a rectangular 100*100 in center of an image?

6 visualizaciones (últimos 30 días)
amir
amir el 17 de Abr. de 2013
Editada: DGM el 25 de Oct. de 2022
i need to creat a rectangular 100*100 in center of an image? i used shapeInserter = vision.ShapeInserter but it`s not what i need. this function creats rectang. on image but i need a rectang. in center of image

Respuesta aceptada

Image Analyst
Image Analyst el 17 de Abr. de 2013
imageArray(row1:row2, col1:col2) = grayValue; % grayValue can be any number.
row1 is half the height minus 50 pixels, col2 is half the width plus 50 pixels, and so on for other parameters.

Más respuestas (1)

DGM
DGM el 25 de Oct. de 2022
Editada: DGM el 25 de Oct. de 2022
If you want to create a rectangular region of a fixed geometry:
% get a grayscale image
inpict = imread('cameraman.tif');
% specify the new color (gray, uint8-scale)
newcolor = 128;
% create a rectangle of fixed geometry
rectsz = [100 150]; % specify a box geometry [h w]
sz = [size(inpict,1) size(inpict,2)];
hw = rectsz/2;
rows = round(sz(1)/2-hw(1)):round(sz(1)/2+hw(1));
cols = round(sz(2)/2-hw(2)):round(sz(2)/2+hw(2));
rows = min(max(rows,1),sz(1));
cols = min(max(cols,1),sz(1));
% assign a value to the ROI
inpict(rows,cols,:) = newcolor;
imshow(inpict)
If you want to create a rectangular region that's proportional to the image geometry:
% get a grayscale image
inpict = imread('cameraman.tif');
% specify the new color (gray, uint8-scale)
newcolor = 128;
% pick a rectangular region that's proportional to the image geometry
w = [0.4 0.6]; % specify a box proportions [h w]
sz = [size(inpict,1) size(inpict,2)];
hw = w.*sz/2;
rows = round(sz(1)/2-hw(1)):round(sz(1)/2+hw(1));
cols = round(sz(2)/2-hw(2)):round(sz(2)/2+hw(2));
rows = min(max(rows,1),sz(1));
cols = min(max(cols,1),sz(1));
% assign a value to the ROI
inpict(rows,cols,:) = newcolor;
imshow(inpict)
Note that this isn't going to be pixel-perfect depending on whether your image/rectangle have odd/even geometry. If you need something to be perfect, you'll have to decide where to make the necessary compromises.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by