This is the 26th day of my participation in the August Text Challenge.More challenges in August

Python OpenCV 365 day learning plan, enter the graphics realm with eraser.

Foundation of basic knowledge

Through the inverse projection of the histogram, A specific image B can be found in image A. In general, image A is relatively large, and image B is relatively small, or image B has only 1 pixel.

You can find the most matched region image or pixel point.

Some of the translation theories are to locate the template image in the input image.

In vernacular translation, it is to calculate the image as a histogram model of a feature, and then use the model to find the feature in the image.

If we go further, there’s a lot of math involved in reverse projection of images, but we won’t go into that at the beginning of our trip.

Cv2. CalcBackProject function

A function called cv2.calcBackProject is provided in Python OpenCV to implement a backprojection.

Let’s learn how to use this function in detail.

Function prototype description

dst = cv2.calcBackProject(images, channels, hist, ranges, scale[, dst])
Copy the code

The parameters are described as follows:

  • images: Enter the image, note add[];
  • channels: channels, the number of channels must match the histogram dimension,
  • hist: histogram of an image;
  • ranges: Range of histogram changes;
  • scale: Outputs an optional scale factor for backprojection.

The case Demo is as follows:

import cv2 as cv
from matplotlib import pyplot as plt

def back_projection_demo() :
    # fetch image
    test = cv.imread("test1.jpg")
    target = cv.imread("target.jpeg")
    Convert to HSV format
    roi_hsv = cv.cvtColor(test, cv.COLOR_BGR2HSV)
    target_hsv = cv.cvtColor(target, cv.COLOR_BGR2HSV)

    cv.imshow("sample", test)
    cv.imshow("target", target)

    # Calculate the histogram
    roiHist = cv.calcHist([roi_hsv], [0.1].None[64.64], [0.180.0.256])
    Get the inverse projection of the histogram
    dst = cv.calcBackProject([target_hsv], [0.1],
                             roiHist, [0.180.0.256].1)
    cv.imshow("back_projection_demo", dst)

back_projection_demo()
cv.waitKey(0)

cv.destroyAllWindows()
Copy the code

So let’s see how it works.

However, this effect is not very good, I added a slider to get more accurate values.

Modify the code to look like this.

import cv2 as cv
from matplotlib import pyplot as plt

winName = 'getTrackbarPos'
Create a new window
cv.namedWindow(winName, cv.WINDOW_NORMAL)

def nothing() :
    pass

cv.createTrackbar('hue', winName, 60.180, nothing)
cv.createTrackbar('sat', winName, 60.256, nothing)

def back_projection_demo() :
    # fetch image
    test = cv.imread("test1.jpg")
    Convert to HSV format
    roi_hsv = cv.cvtColor(test, cv.COLOR_BGR2HSV)

    target = cv.imread("target.jpeg")
    target_hsv = cv.cvtColor(target, cv.COLOR_BGR2HSV)

    cv.imshow("sample", test)
    cv.imshow("target", target)
    while(1) :Read data from the slider
        hue = cv.getTrackbarPos('hue', winName)
        sat = cv.getTrackbarPos('sat', winName)

        # Calculate the histogram
        roiHist = cv.calcHist([roi_hsv], [0.1].None, [
            hue, sat], [0.180.0.256])

        Get the inverse projection of the histogram
        dst = cv.calcBackProject([target_hsv], [0.1],
                                 roiHist, [0.180.0.256].1)
        cv.imshow(winName, dst)
        if cv.waitKey(1) = =ord('q') :break

back_projection_demo()
cv.destroyAllWindows()
Copy the code

After repeated adjustment, it was found that the histogram reverse projection, in addition to the eraser to increase a function of cognition, and did not produce too much thinking, may not learn enough in-depth, in short, the function income in the brain, when it appears again in the future, we take it out in taste.

Add the official address: click here

Eraser section

I hope you got something out of your hour today, and we’ll see you in the next blog

reading



Today is the 69/100 day of continuous writing. If you have ideas or techniques you’d like to share, feel free to leave them in the comments section.


Blogger ID: Dream eraser, hope you like, comment, favorites.