[英]Crop detected image via OpenCV
Can someone help me on how to crop the detected portion of the image.有人可以帮助我如何裁剪检测到的图像部分。 I am struggling to pick the correct x and y axis.
我正在努力选择正确的 x 和 y 轴。 I wish to know how to pass the detected boundary X and Y axis to the image to crop.
我想知道如何将检测到的边界 X 和 Y 轴传递给图像以进行裁剪。
Note: The Aim is to crop exactly the detected image for comparing the template vs detected image.注意:目的是准确裁剪检测到的图像,以比较模板与检测到的图像。 I am aware of how to crop and compare an image.
我知道如何裁剪和比较图像。 Problem is to find the exactly boundary that are detected in the original image.
问题是找到在原始图像中检测到的确切边界。
Code to detect:检测代码:
import cv2
import numpy as np
img_rgb = cv2.imread('Foo_1.png')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('template.PNG', 0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 255, 255), 2)
cv2.imshow('Detected', img_rgb)
crop_img = img_rgb[y:y+h, x:x+w] --> Unclear about how to pass the x nd y axis values automatically from detected boundary.
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)
Image_1.png Image_1.png
template.png模板.png
Expected Output:- The detected image must to be cropped and match with template image.预期的 Output:- 检测到的图像必须被裁剪并与模板图像匹配。
The detected image is cropped and matches the image template.检测到的图像被裁剪并与图像模板匹配。
import cv2
img_rgb = cv2.imread('Image_1.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('template.png', cv2.IMREAD_GRAYSCALE)
w, h = template.shape
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
_, _, _, maxLoc=cv2.minMaxLoc(res)
cv2.rectangle(img_rgb, maxLoc, (maxLoc[0]+h, maxLoc[1]+w), (0, 255, 255), 2)
cv2.imshow('Detected', img_rgb)
crop_img = img_rgb[maxLoc[1]:maxLoc[1]+w, maxLoc[0]:maxLoc[0]+h, :]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
This code below will help you use the mouse events to record the co-ordinates n下面的这段代码将帮助您使用鼠标事件来记录坐标 n
# import the necessary packages
import argparse
import cv2
# initialize the list of reference points and boolean indicating
# whether cropping is being performed or not
ref_point = []
cropping = False
def shape_selection(event, x, y, flags, param):
# grab references to the global variables
global ref_point, cropping
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
ref_point = [(x, y)]
cropping = True
# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates and indicate that
# the cropping operation is finished
ref_point.append((x, y))
cropping = False
# draw a rectangle around the region of interest
cv2.rectangle(image, ref_point[0], ref_point[1], (0, 255, 0), 2)
cv2.imshow("image", image)
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
args = vars(ap.parse_args())
# load the image, clone it, and setup the mouse callback function
image = cv2.imread(args["image"])
clone = image.copy()
cv2.namedWindow("image")
cv2.setMouseCallback("image", shape_selection)
# keep looping until the 'q' key is pressed
while True:
# display the image and wait for a keypress
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
# if the 'r' key is pressed, reset the cropping region
if key == ord("r"):
image = clone.copy()
# if the 'c' key is pressed, break from the loop
elif key == ord("c"):
break
# if there are two reference points, then crop the region of interest
# from teh image and display it
if len(ref_point) == 2:
crop_img = clone[ref_point[0][1]:ref_point[1][1], ref_point[0][0]:ref_point[1][0]]
cv2.imshow("crop_img", crop_img)
cv2.waitKey(0)
# close all open windows
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.