Python opencv histogram reverse projection method, pythonopencv

Source: Internet
Author: User

Python opencv histogram reverse projection method, pythonopencv

This article introduces the python opencv histogram reverse projection method, which is described as follows:

Objectives:

Histogram reverse projection

Principle:

Reverse projection can be used for image segmentation to search for interesting ranges. It outputs an image of the same size as the input image. Each pixel value represents the probability that the corresponding Vertex on the input image belongs to the target object. In short, the higher the pixel value in the output image, the more likely the point represents the target to be searched. Histogram projection is often used with the camshift algorithm.

To implement the algorithm, first create a histogram for the images that contain the areas of interest (for example, we need to find a lawn, but not others ). The searched object should preferably occupy the entire image (the image is full of lawns ). It is best to use a color histogram. The color information of an object is easier to be divided and recognized than that of a gray image. Then, the color histogram is projected to the search Target of the input image, that is, the probability of finding the pixel value of each pixel in the input image in the histogram is obtained, finally, set the appropriate threshold value to binarization the probability image.

Numpy algorithm:

Create two color histograms, the target image histogram (M), and the input image histogram (I)

Import cv2import numpy as npfrom matplotlib import pyplot as plt # roi image, you need to find the Image roi = cv2.imread('3.jpg ') hsv = cv2.cvtColor (roi, cv2.COLOR _ BGR2HSV) # target search image target = cv2.imread('33.jpg ') hsv T = cv2.cvtColor (target, cv2.COLOR _ BGR2HSV) # create a histogram M = cv2.calcHist ([hsv], [180,256], None, [], [0,180, 0,256]) I = cv2.calcHist ([HSV T], [180,256], None, [0,180], [0,256,])

Calculation ratio: R = MI. Reverse projection R: Creates a new image based on the R palette. Each pixel represents the probability of the event target. For example, B (x, y) = R [h (x, y), s (x, y), where H is the hue of the vertex (x, y) value. s is the saturation (saturation) of the vertex (x, y ). Add Condition B (x, y) = min ([B (x, y), 1]

h,s,v = cv2.split(hsvt)B = R[h.ravel(),s.ravel()]B = np.minimum(B,1)B = B.reshape(hsvt.shape[:2])

Uses the disc operator for convolution. B = D × B, where D is the convolution kernel.

Disc = cv2.getStructuringElement (cv2.MORPH _ ELLIPSE, (5, 5) # define the structure shape, 5 × 5 elliptical B = cv2.filter2D (B,-1, disc) # convolution of images B = np. uint8 (B) cv2.normalize (B, B, 0,255, cv2.NORM _ MINMAX)

The function cv2.getStructuringElement is used to define structural elements. For example, element = cv2.getStructuringElement (cv2.MORPH _ CROSS, (5, 5) defines a CROSS-shaped, 5 × 5 structure.

 

The biggest gray scale in the output image is the target position. If you are looking for a region, you can use a threshold to binarization the image. This will produce good results.

ret,thresh = cv2.threshold(B,50,255,0)

Opencv reverse projection

The cv2.calcBackProject () function directly implements reverse projection. The parameters are basically the same as those of cv2.calcHist. One of the parameters is the histogram of the target to be searched. When using the target histogram, the reverse payment should be normalized. The returned result is a probability image, then the disc shape convolution operation, and then binarization.

Roi region Image

 

Image to be searched

 

Result

Import cv2import numpy as npfrom matplotlib import pyplot as plt # roi image, you need to find the Image roi = cv2.imread('33.jpg ') hsv = cv2.cvtColor (roi, cv2.COLOR _ BGR2HSV) # target search image target = cv2.imread('3.jpg ') hsv T = cv2.cvtColor (target, cv2.COLOR _ BGR2HSV) # Calculate the target histogram roihist = cv2.calcHist ([hsv], [0, 1], None, [180,256], [0,180, 0,256]) # Normalization: the parameter is the original image and the output image. After normalization, all values are in the range of 2 to 255 cv2.normalize (roihist, roihist, 0,255, cv2.NORM _ MINMAX) dst = cv2.calcBackProject ([hsv t], [0,180], roihist, [0,256,], 1) # disc = cv2.getStructuringElement (cv2.MORPH _ ELLIPSE, (5, 5) dst = cv2.filter2D (dst,-1, disc) ret, thresh = cv2.threshold (dst, 50,255, 0) # Use merge to convert the channel image thresh = cv2.merge (thresh, thresh, thresh) # mask res = cv2.bitwise _ and (target, thresh) # merge matrices by column, that is, target, thresh and res are combined horizontally. res = np.hstack((target,thresh,res?cv2.imwrite('res.jpg ', res) # Show image cv2.imshow ('1', res) cv2.waitKey (0)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.