def back_projection_demo(target):
    sample = target[430:442, 305:317, :3]  # 建筑墙面色调块
    roi_hsv = cv.cvtColor(sample, cv.COLOR_BGR2HSV)
    target_hsv = cv.cvtColor(target, cv.COLOR_BGR2HSV)
    cv.imshow("sample", sample)
    cv.imshow("target", target)
    roihist = cv.calcHist([roi_hsv], [0, 1], None, [8, 8],
                          [0, 180, 0, 256])  # [180, 256] binsize小则查找粗糙
    cv.normalize(roihist, roihist, 0, 255, cv.NORM_MINMAX)  # 归一化
    dst = cv.calcBackProject([target_hsv], [0, 1], roihist, [0, 180, 0, 256],
                             1)
    cv.imshow("backproject", dst)
Exemple #2
0
def hist_extract(image, handHist):
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    dst = cv2.calcBackProject([hsv], [0, 1], handHist, [0, 180, 0, 256], 1)
    disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (21, 21))
    cv2.filter2D(dst, -1, disc, dst)
    # dst is now a probability map
    # Use binary thresholding to create a map of 0s and 1s
    # 1 means the pixel is part of the hand and 0 means not
    ret, thresh = cv2.threshold(dst, 150, 255, cv2.THRESH_BINARY)
    kernel = np.ones((5, 5), np.uint8)
    thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=8)
    #thresh = cv2.merge((thresh, thresh, thresh))
    return thresh
Exemple #3
0
def back_projection_demo():#反向投影
    roi = cv.imread("C:/Users/32936/Desktop/2/qiuyi.jpg")#搜索目标
    target = cv.imread("C:/Users/32936/Desktop/2/maidi.jpg")#待搜索图片
    hsv = cv.cvtColor(roi,cv.COLOR_BGR2HSV)
    hsvt = cv.cvtColor(target,cv.COLOR_BGR2HSV)
    roihist = cv.calcHist([hsv],[0,1],None,[32,32],[0,180,0,256])#获取搜索目标直方图
    cv.normalize(roihist,roihist,0,255,cv.NORM_MINMAX)#直方图归一化
    mask = cv.calcBackProject([hsvt],[0,1],roihist,[0,180,0,256],3)#直方图反向投影
    dst = cv.bitwise_and(target,target,mask=mask)
    cv.imshow("result",dst)
    cv.imshow("target",target)
    cv.imshow("image",roi)
    cv.imshow("mask",mask)
Exemple #4
0
# set up the ROI for tracking
roi = frame[y:y + h, x:x + w]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)),
                   np.array((180., 255., 255.)))
roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
# setup the termination criteria, either 10 iteration or move by atleast 1 pt
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
cv2.imshow('roi', roi)
while (1):
    ret, frame = cap.read()
    if ret == True:

        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
        # apply meanshift to get the new location
        ret, track_window = cv2.meanShift(dst, track_window, term_crit)
        # Draw it on image
        x, y, w, h = track_window
        final_image = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0),
                                    3)

        cv2.imshow(' final_image', final_image)
        cv2.imshow('dst', dst)
        k = cv2.waitKey(30) & 0xFF
        if k == 27:
            break
    else:
        break
# OpenCV provides an inbuilt function cv2.calcBackProject(). Its parameters are almost same as the cv2.calcHist() function. One of its parameter is histogram which is histogram of the object and we have to find it. Also, the object histogram should be normalized before passing on to the backproject function. It returns the probability image. Then we convolve the image with a disc kernel and apply threshold. Below is my code and output :

from cv2 import cv2
import numpy as np

img = cv2.imread('resource/messi.jpg')
roi = img.copy()[222:278, 2:77]

hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

hist_roi = cv2.calcHist([hsv_roi], [0, 1], None, [180, 256], [0, 180, 0, 256])

cv2.normalize(hist_roi, hist_roi, 0, 255, cv2.NORM_MINMAX)
backproject = cv2.calcBackProject([hsv_img], [0, 1], hist_roi,
                                  [0, 180, 0, 256], 1)

# Now convolute with circular disc
disckernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
circular = backproject.copy()
cv2.filter2D(backproject, -1, disckernel, circular)

# threshold and binary AND
ret, thresh = cv2.threshold(circular, 50, 255, 0)
thresh = cv2.merge((thresh, thresh, thresh))
res = cv2.bitwise_and(img, thresh.copy())

cv2.imshow('origin', img)
cv2.imshow('roi', roi)
cv2.imshow('back', backproject)
cv2.imshow('circular', circular)
Exemple #6
0
    def start_tracking(self):
        # Iterate until the user presses the Esc key
        while True:
            # Capture the frame from webcam
            _, self.frame = self.cap.read()

            # Resize the input frame
            self.frame = cv.resize(self.frame,
                                   None,
                                   fx=self.scaling_factor,
                                   fy=self.scaling_factor,
                                   interpolation=cv.INTER_AREA)

            # Create a copy of the frame
            vis = self.frame.copy()

            # Convert the frame to HSV colorspace
            hsv = cv.cvtColor(self.frame, cv.COLOR_BGR2HSV)

            # Create the mask based on predefined thresholds
            mask = cv.inRange(hsv, np.array((0., 60., 32.)),
                              np.array((180., 255., 255.)))

            # Check if the user has selected the region
            if self.selection:
                # Extract the coordinates of the selected rectangle
                x0, y0, x1, y1 = self.selection

                # Extract the tracking window
                self.track_window = (x0, y0, x1 - x0, y1 - y0)

                # Extract the regions of interest
                hsv_roi = hsv[y0:y1, x0:x1]
                mask_roi = mask[y0:y1, x0:x1]

                # Compute the histogram of the region of
                # interest in the HSV image using the mask
                hist = cv.calcHist([hsv_roi], [0], mask_roi, [16], [0, 180])

                # Normalize and reshape the histogram
                cv.normalize(hist, hist, 0, 255, cv.NORM_MINMAX)
                self.hist = hist.reshape(-1)

                # Extract the region of interest from the frame
                vis_roi = vis[y0:y1, x0:x1]

                # Compute the image negative (for display only)
                cv.bitwise_not(vis_roi, vis_roi)
                vis[mask == 0] = 0

            # Check if the system in the "tracking" mode
            if self.tracking_state == 1:
                # Reset the selection variable
                self.selection = None

                # Compute the histogram back projection
                hsv_backproj = cv.calcBackProject([hsv], [0], self.hist,
                                                  [0, 180], 1)

                # Compute bitwise AND between histogram
                # backprojection and the mask
                hsv_backproj &= mask

                # Define termination criteria for the tracker
                term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10,
                             1)

                # Apply CAMShift on 'hsv_backproj'
                track_box, self.track_window = cv.CamShift(
                    hsv_backproj, self.track_window, term_crit)

                # Draw an ellipse around the object
                cv.ellipse(vis, track_box, (0, 255, 0), 2)

            # Show the output live video
            cv.imshow('Object Tracker', vis)

            # Stop if the user hits the 'Esc' key
            c = cv.waitKey(5)
            if c == 27:
                break

        # Close all the windows
        cv.destroyAllWindows()
# OpenCV provides an built-in function cv2.calcBackProject(). Its parameters are almost same as the cv2.calcHist() function. One of its parameter is histogram which is histogram of the object and we have to find it. Also, the object histogram should be normalized before passing on to the backproject function. It returns the probability image. Then we convolve the image with a disc kernel and apply threshold.

roi = cv2.imread(r'pictures\roi.a.2x.jpg')
hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

target = cv2.imread(r'pictures\a.2x.jpg')
hsvt = cv2.cvtColor(target, cv2.COLOR_BGR2HSV)
target_rgb = cv2.cvtColor(target, cv2.COLOR_BGR2RGB)

# calculating object histogram
roihist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])

# normalize histogram and apply backprojection
cv2.normalize(roihist, roihist, 0, 255, cv2.NORM_MINMAX)
dst = cv2.calcBackProject([hsvt], [0, 1], roihist, [0, 180, 0, 256], 1)

# Now convolute with circular disc
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
cv2.filter2D(dst, -1, disc, dst)

# threshold and binary AND
ret, thresh = cv2.threshold(dst, 50, 255, 0)
thresh = cv2.merge((thresh, thresh, thresh))
res = cv2.bitwise_and(target_rgb, thresh)

# res = np.vstack((target,thresh,res))
# cv2.imwrite('res.jpg',res)
plt.subplot(311), plt.imshow(target_rgb)
plt.subplot(312), plt.imshow(thresh)
plt.subplot(313), plt.imshow(res)
Exemple #8
0
lower_bound = np.array([15, 100, 100])
upper_bound = np.array([35, 255, 255])
pts = []
#reg for recatangle X= 400 to 640 ,Y= 0 to 200
#pts [i][0]=x,pts[i][1]=y
while True:
    ret, frame = cap.read()
    frame = cv2.flip(frame, 1)
    temp_frame = frame
    flag = 0
    hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv_frame, lower_bound, upper_bound)
    roi_hist = cv2.calcHist([hsv_frame], [0], mask, [180], [0, 180])
    roi_norm_hist = cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
    termination = (cv2.TERM_CRITERIA_EPS | cv2.TermCriteria_COUNT, 40, 10)
    dst = cv2.calcBackProject([hsv_frame], [0], roi_norm_hist, [0, 180], 1)
    ret, track_window = cv2.CamShift(dst, track_window, termination)
    x, y, w, h = track_window
    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
    center = int((2 * x + w) / 2), int((2 * y + h) / 2)
    pts.append(center)
    if center[0] in range(500, 640) and center[1] in range(0, 150):
        cv2.rectangle(temp_frame, (500, 0), (640, 150), (0, 255, 0), -1)
        cv2.putText(temp_frame, "ERASED", (510, 70), cv2.FONT_HERSHEY_PLAIN, 2,
                    (255, 0, 0), 2)
        temp_frame = cv2.resize(temp_frame, (1024, 768),
                                interpolation=cv2.INTER_LANCZOS4)
        cv2.imshow("tracking", temp_frame)
        flag = 1
        pts = []
    for i in range(1, len(pts)):