Python OpenCV learning notes histogram reverse projection implementation, pythonopencv

Source: Internet
Author: User

Python OpenCV learning notes histogram reverse projection implementation, pythonopencv

This article introduces the implementation of the reverse projection of the python OpenCV learning note histogram. The details are as follows:

Documents-https://docs.opencv.org/3.4.0/dc/df6/tutorial_py_histogram_backprojection.html

It is used for image segmentation or searching for objects of interest in the image. Simply put, it creates an image of the same size (but single channel) as our input image, where each pixel corresponds to the probability of the pixel that belongs to our object. The output image will make the objects we are interested in more white than the rest.

What should we do? We create an image histogram that contains the objects we are interested in. To get better results, the object should be filled with images as much as possible. The color histogram is more favored than the gray histogram, because the color of an object is more customizable than the gray intensity. Then, we "reverse projection" the histogram on our test image, and we need to find this object. In other words, we calculate the probability of each pixel and display it. The output result generated at the appropriate threshold gives us a separate result.

Numpy Algorithm

1. First, we need to calculate the color histogram of the object we need to find (Make It 'M') and the image we will search for (Make It 'I ').

Import numpy as npimport cv2 as cvfrom matplotlib import pyplot as plt # roi is the object we need to find or region roi = cv.imread('rose_red.png ') hsv = cv. cvtColor (roi, cv. COLOR_BGR2HSV) # target is the image we searched for. target = cv.imread('rose.png ') hsv t = cv. cvtColor (target, cv. COLOR_BGR2HSV) # Use calcHist to find the histogram, or use np. histogram2dM = cv. calcHist ([hsv], [180,256], None, [0,180], [0,256,]) I = cv. calcHist ([hsv t], [180,256], None, [0,180], [0,256,])

2. Find the ratio R = M/I. Then, Project R on the back, use R as the palette, and create a new image. Each pixel serves as its target probability. B (x, y) = R [h (x, y), s (x, y)], where h is the color of the (x, y) Coordinate pixel, s is the saturation. Then, B (x, y) = min [B (x, y), 1]

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

3. Apply a disc convolution, B = D * B, where D is the disc Kernel

disc = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5,5))cv.filter2D(B, -1, disc, B)B = np.uint8(B)cv.normalize(B, B, 0, 255, cv.NORM_MINMAX)

4. Now, the position of the maximum intensity gives us the position of the object. If we expect a region in the image, a suitable threshold value will produce a good result.

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

Projection in OpenCV

OpenCV provides a built-in function cv. calcbackproject (). Its parameters are almost the same as the cv. calcHist () function. One of its parameters is a histogram, which is the histogram of this object. We must find it. In addition, before being passed to the backproject function, the object's histogram should be standardized. It returns a probability image. Then, we convolution the image with the disk kernel and apply the threshold value. The following is my code and output:

Import numpy as npimport cv2 as cvroi = cv.imread('rose_red.png ') hsv = cv. cvtColor (roi, cv. COLOR_BGR2HSV) target = cv.imread('rose.png ') hsv t = cv. cvtColor (target, cv. COLOR_BGR2HSV) # Calculate the roihist of the object = cv. calcHist ([hsv], [180,256], None, [0,180], [0,256,]) # standardize the histogram and apply the projection cv. normalize (roihist, roihist, 0,255, cv. NORM_MINMAX) dst = cv. calcBackProject ([hsv t], [0,180], roihist, [0,256,], 1) # perform convolution disc = cv with the disk kernel. getStructuringElement (cv. MORPH_ELLIPSE, (5, 5) cv. filter2D (dst,-1, disc, dst) # threshold, binary bitwise, and Operation ret, thresh = cv. threshold (dst, 50,255, 0) thresh = cv. merge (thresh, thresh, thresh) res = cv. bitwise_and (target, thresh) res = np. vstack (target, thresh, resw.cv.imwrite('res.jpg ', res)

The following is an example. Use the area in the blue rectangle as the example object to extract all the content.

For the principles of these two technologies, refer to the link I posted above. The following is the sample code:

0x01. Draw a Histogram

import cv2.cv as cv def drawGraph(ar,im, size): #Draw the histogram on the image  minV, maxV, minloc, maxloc = cv.MinMaxLoc(ar) #Get the min and max value  hpt = 0.9 * histsize  for i in range(size):    intensity = ar[i] * hpt / maxV #Calculate the intensity to make enter in the image    cv.Line(im, (i,size), (i,int(size-intensity)),cv.Scalar(255,255,255)) #Draw the line    i += 1 #---- Gray imageorig = cv.LoadImage("img/lena.jpg", cv.CV_8U) histsize = 256 #Because we are working on grayscale pictures which values within 0-255 hist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1) cv.CalcHist([orig], hist) #Calculate histogram for the given grayscale picture histImg = cv.CreateMat(histsize, histsize, cv.CV_8U) #Image that will contain the graph of the repartition of valuesdrawGraph(hist.bins, histImg, histsize) cv.ShowImage("Original Image", orig)cv.ShowImage("Original Histogram", histImg)#--------------------- #---- Equalized imageimEq = cv.CloneImage(orig)cv.EqualizeHist(imEq, imEq) #Equlize the original image histEq = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1)cv.CalcHist([imEq], histEq) #Calculate histogram for the given grayscale pictureeqImg = cv.CreateMat(histsize, histsize, cv.CV_8U) #Image that will contain the graph of the repartition of valuesdrawGraph(histEq.bins, eqImg, histsize) cv.ShowImage("Image Equalized", imEq)cv.ShowImage("Equalized HIstogram", eqImg)#-------------------------------- cv.WaitKey(0)

0x02. Reverse projection

import cv2.cv as cv im = cv.LoadImage("img/lena.jpg", cv.CV_8U) cv.SetImageROI(im, (1, 1,30,30)) histsize = 256 #Because we are working on grayscale pictureshist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1)cv.CalcHist([im], hist)cv.NormalizeHist(hist,1) # The factor rescale values by multiplying values by the factor_,max_value,_,_ = cv.GetMinMaxHistValue(hist) if max_value == 0:  max_value = 1.0cv.NormalizeHist(hist,256/max_value) cv.ResetImageROI(im) res = cv.CreateMat(im.height, im.width, cv.CV_8U)cv.CalcBackProject([im], res, hist) cv.Rectangle(im, (1,1), (30,30), (0,0,255), 2, cv.CV_FILLED)cv.ShowImage("Original Image", im)cv.ShowImage("BackProjected", res)cv.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.