how to creat a rectangular 100*100 in center of an image?
Show older comments
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
Accepted Answer
More Answers (1)
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.
Categories
Find more on Computer Vision Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
