The basic idea of target detection based on matlab

The basic idea of ​​target detection

For a picture with a clear outline, you can first detect its outline. At this time, the outline of the background will also be added in. You can fill in the hole first, then corrode and expand to eliminate the excess edges to find the outline of the target. For pictures with unclear outlines or complex backgrounds, you can use image binarization to filter out the required targets with a specific threshold, or divide the image into blocks, and then perform morphological operations to make the outline more specific.

Problems encountered

deer

For a color image of a particular color to be extracted is more difficult to select the threshold value, the target color of the image is not unique, a color and black and white, by converting rgb space ycbcr space, were selected on the threshold ycbcr
FIG.
Insert picture description here

The outline of the deer can be roughly identified, and then the image is dilated and corroded to eliminate the small connected domains to obtain the outline
corrosion, and the largest connected domain is selected as shown in the figure

Some places are corroded.
code show as below:

y_min=0;y_max=256;
cb_min= 90;cb_max=150 ;
cr_min=124 ;cr_max= 180;
 
roi=roicolor(img_ycbcr(:,:,1),y_min,y_max) & roicolor(img_ycbcr(:,:,2),cb_min,cb_max) & roicolor(img_ycbcr(:,:,3),cr_min,cr_max);

people

After the image is binarized, it is found that there is obvious interference information in the lower half of the image, so it is decided to divide the image into two parts to be processed. Since there is no target to be recognized in the lower half, the original image can be displayed in the lower half. After the upper part is binarized, because it has a big difference, the target can be displayed more clearly, as shown in the figure
Insert picture description here

The follow-up processing is similar to the above figure and the
result is shown in the figure
Insert picture description here

Divide the picture into two pieces:

img=imread('people.jpg');
img1=imcrop(img,[0,0,400,170]);
img2=imcrop(img,[0,171,400,304]);

Rest normal processing

eagle

There is a big difference between the target and the background in this picture, which is better for detection.
At the beginning, you can select the threshold when binarizing, and then use the canny operator to extract the edge
Insert picture description here

Although there is a background line, it is not a connected domain. Fill the connected domain with imfill and then corrode it to eliminate this line
. The result after edge extraction is shown in the figure
Insert picture description here
Insert picture description here

code show as below:

img_bw=im2bw(img_gray,0.8);
 
img_edge=edge(img_bw,'canny');
%imshow(img_edge);
dil_filter=strel('square',2);%膨胀一下
img_edge=imdilate(img_edge,dil_filter);
 
img_edge=bwperim(img_edge);
img_full=imfill(img_edge,'holes');
 
 
ero_filter=strel('square',3);
img_full=imerode(img_full,ero_filter);
img_full=bwareaopen(img_full,50);
 
out_edge=edge(img_full,'canny');
out_edge=imdilate(out_edge,dil_filter);

flower

The red color of the flower can actually be separated by r or g in rgb, but it can also be directly converted into a binary image to set a threshold for separation. Small leaves will be detected, and there will be spots after corrosion. You only need to eliminate small The connected domain is sufficient. As shown
Insert picture description here

dog

Similar to flowers, the first step is to use threshold segmentation to achieve better results
Insert picture description here

to sum up

Using threshold method segmentation can get most of the better results. My understanding of this threshold is that in a grayscale image, the first derivative of the image of an area with constant grayscale value is zero until the grayscale value changes. The first-order derivative is greater than or less than zero, and the existence of the threshold is to select a standard for determining when the first-order derivative is not zero, and the pixel value of the gray image is determined by the three values ​​of RGB of the color image. Generally It is calculated by the traditional formula Gray= R 0.299+G 0.587+B*0.114. It can be seen that the proportion of G is the largest and the proportion of B is the smallest. So in the result of converting the gray image, if the original image has green information or red The information is more sensitive, so you can get better results with one-step segmentation when performing threshold segmentation, such as a picture of a flower. In addition, the color difference in the RGB image itself is very large, so that the values ​​of the three RGB channels are all very different, and better results can be obtained, such as a picture of a dog. Such a picture is more suitable for threshold segmentation.
Without such a congenital picture, RGB does not have good specific color information of the object, so I choose to use the ycbcr color space for threshold selection, where Y represents the brightness and density of the color, and Cb and Cr respectively represent The blue density offset and the red density offset of the color. You can define a range to select the desired area, so that the effect of binarization is much better.

appendix

Filter h=fspecial(type,parameters); parameters are optional and are
configuration parameters related to the selected filter type , such as size and standard deviation.
type is the type of filter. The legal values ​​are as follows:

Legal value function'average
' average template'disk' average template of
circular
area'gaussian' Gaussian template'laplacian
' Laplacian template'log
' Gauss-Laplacian template'prewitt
' Prewitt horizontal edge detection operator
'sobel' Sobel horizontal edge detection operator

Median filtering h=medfilt2(I1,[m,n]);
structure element function strel(shape, parameters); shape specifies the shape of the structure element. parameters are the parameters related to the input shape.

Legal value Function description'arbitrary
' or empty Any custom structure
element'disk' Round structure
element'square' Square structure
element'rectangle' Rectangular structure
element'line' Linear structure
element'pair' A structure containing 2 points The element'diamond'
The structure element of the
diamond'octagon' The structure element of the octagon

Expansion I2 = imdilate (I, SE) ; SE = strel (shape, parameters);
Corrosion I2 = imerode (I, SE) ;
opening operation I2 = imopen (I, SE) ; eliminate small objects
closing operation I3 = imclose (I ,SE);Fill small holes, connect neighboring objects,
hit and miss transform Ihm=bwhitmiss(I,SE1,SE2);

Morphological processing Iout=bwmorph(I,operation,n)

Legal value function description'bridge
' bridges foreground pixels divided by a single pixel
gap'clean' clearly isolated foreground pixels'diag'
fills the diagonally connected foreground pixels'fill'
fills the holes of a single
pixel'hbreak' removes H-shaped connection in the foreground'majority
' If more than half of the pixels in the 8 area of ​​point P are foreground pixels, then P is the foreground pixel, otherwise it is the background.
'remove' inside the pixel is removed (no background pixels adjacent to the foreground)
'Shrink' will shrink to a point object or endless belt holes
' skel 'skeletonized image
' spur 'remove' glitches'
'thicken' roughening object
'thin 'Refine the object to the minimum connected line

Edge detection BW=edge(I,type,thresh,direction,'nothinning') thresh is the sensitivity threshold parameter, any edge with gray value lower than this threshold will not be detected. The default value is an empty matrix [], at this time the algorithm automatically calculates the threshold.

Type legal value gradient operator'sobel
' sobel operator'prewitt
' prewitt operator'reberts
' robert operator

Edge detection based on Gaussian-Laplacian operator BW=edge(I,'log',thresh,sigma) sigma specifies the standard deviation used to generate the Gaussian filter. By default, the standard deviation is 2.
Edge detection based on Canny operator BW=edge(I,'canny',thresh,sigma)

Guess you like

Origin blog.csdn.net/qq_36587495/article/details/108164684