how to creat a rectangular 100*100 in center of an image?
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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.
0 个评论
更多回答(1 个)
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!