Original articles, welcome reprint. Reprint Please specify: Reproduced from Cheung's blog
Original link: http://blog.csdn.net/humanking7/article/details/46819527
In the graduation design time to write a paper drawing, in an image to accurately mark the area to be processed, with Photoshop or other graphics software is not accurate, but also does not meet the lazy thinking (how to do, once and for all, hey), so write software yourself.
Programming Ideas and considerations
In fact, the idea of the program is very simple, that is, the local area of the pixel value (color) covered out on the line, but pay attention to a few points.
- Original image channel problem (border color problem). Whether the image is single-channel (black-and-white) or multi-channel (color), this is the color of the box you want, my method is that if it is a single-channel diagram, it is now processed as multi-channel.
- Boundary problems. The border is width, and my box is filled out, so take into account the image boundary problem, if you go beyond the error (the image in the form of an array in MATLAB, out of range of the Out of bounds)
- The pixel point position. The image is stored in the form of an array in MATLAB, in the image pixel coordinate system, the x direction is represented by the array's columns , and the y direction is represented by the array's rows . Be careful when taking image elements.
Draw a Rectangle Box program
Save As drawRect.m file, this is a function file.
function [dest] = drawrect(SRC, PT, wsize, linesize, color) % Introduction:% The image is painted with a color block diagram, if the input is a grayscale image, first converted to a color image, then a block diagram% Image Matrix% Line vector direction is ythe% column vector direction is x%----------------------------------------------------------------------% Input:% src: Original image, can be grayscale, can be color graph% PT: Upper-left coordinate [x1, y1]% wsize: Box size [WX, WY]% linesize: width of line% Color: line Color [R, G, b]%----------------------------------------------------------------------% Output:% dest: Pictures that have been painted well%----------------------------------------------------------------------%flag=1: The box with a gap%flag=2: Non-notched boxFlag =1;% determine the number of input parametersifNargin <5color =[255 255 0];EndifNargin <4Linesize =1;EndifNargin <3 disp(' input parameters not enough!!! ');return;Endthe boundary problem of the% judgment box[YA, XA, z]=size(src); x1 = PT (1); y1 = pt (2); WX = Wsize (1); WY = Wsize (2);ifX1>xa | | ... y1>ya| | ... (X1+WX) >xa| | ... (Y1+wy) >yadisp(' The frame will be more than the picture!!! ');return;End% If it is a single-channel grayscale, turn into a 3-channel imageif 1==z dest (:,:,1) = src; Dest (:,:,2) = src; Dest (:,:,3) = src;ElseDest = src;End% start Drawing block Diagram forc =1:3 % of 3 channels, R,g,b painted separately forDL =1: linesizeThe width of the line, the line is extended to the outsideD = DL-1;if 1==flag% box with notchDest (y1-d, X1: (X1+WX), c) = Color (c);% above lineDest (Y1+wy+d, X1: (X1+WX), c) = Color (c);% lower LineDest (y1: (Y1+wy), x1-d, c) = Color (c);% left lineDest (y1: (Y1+wy), x1+wx+d, c) = Color (c);% left line ElseIf 2==flag% non-notched boxDest (y1-d, (x1-d):(x1+wx+d), c) = Color (c);% above lineDest (Y1+wy+d, (x1-d):(x1+wx+d), c) = Color (c);% lower LineDest ((y1-d):(y1+wy+d), x1-d, c) = Color (c);% left lineDest ((y1-d):(y1+wy+d), x1+wx+d, c) = Color (c);% left line End End End % main Cycle endEnd % function End
Calling the main program
drawRect.mthe function in the call drawRect .
Clc;clear;close all;%-----------------------------------% Add a rectangle to the image%-----------------------------------[filename, pathname] = Uigetfile ({' *.jpg ';' *.bmp ';' *.gif ';' *.png '},' Select Picture ');% No imageiffilename = =0 return;Enddata = Imread ([pathname, filename]); [M, n, z] = size (data);p t = [185,273];wsize = [ -, -];d es = DrawRect (data,pt,wsize,5); subplot (1,2,1) imshow (data) subplot (1,2,2) Imshow (DES)return;
Working with result border styles 1
There is a gap in the border style in the drawRect.m file flag = 1 .
Border Style 2
No notch border style in the drawRect.m file flag = 2 .
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[image] use Matlab to draw a rectangle on an image