필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
amir
amir 2013년 4월 17일
편집: DGM 2022년 10월 25일
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

채택된 답변

Image Analyst
Image Analyst 2013년 4월 17일
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.

추가 답변 (1개)

DGM
DGM 2022년 10월 25일
편집: DGM 2022년 10월 25일
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.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by