OpenCV rectangle fill & stroke in a single loop
Introduction
Hi! 我试图在开放式CV和Av的模版配对上添加一条斜体,并用透光填充和坚固的中风。 从事这项工作的材料Im来自一个名为:Sun Haven ,由Pixel Sprout生产商在
<<>Operating System: 微软Windows 11 <>Python>:3.10.4 <><0>>>>> 目前,我有一份将作trick的文字,但我不喜欢它的执行方式,因为我现在不得不两次通过模板结果: 我倾向于将两种“试金星”的旋转体合并成单一循环,但如果我这样做,我只拿到填料,而不是拿到结果的中风。 如你所看到的那样,这发现并凸显了原形象中的四种贝.,但我却把两种环状混为一谈,不重复直径和性能。 我试图将这两处 lo合成一: However, this does not yield the results I m looking for, as it will only draw the transparent fill, not the stroke:
So am I stuck with two almost identical for-loops, or is there a way to do this in a single loop?
I d really like to not repeat something that only needs to be done once, but if I have to, I have to. 期待反馈,ers!Setup
Problem
import cv2 as cv
import numpy as np
# Load the haystack and needle images in OpenCV.
haystack_image = cv.imread( sun_haven_14.jpg , cv.IMREAD_UNCHANGED)
needle_image = cv.imread( sun_haven_beecube.jpg , cv.IMREAD_UNCHANGED)
# Haystack image with drawings on.
scribble_image = haystack_image.copy()
# Grab the dimensions of the needle image.
needle_width = needle_image.shape[1]
needle_height = needle_image.shape[0]
# Match the needle image against the the haystack image.
result = cv.matchTemplate(haystack_image, needle_image, cv.TM_CCOEFF_NORMED)
# Define a standard to which a result is assumed positive.
threshold = 0.5
# Filter out image locations where OpenCV falls below the confidence threshold.
locations = np.where(result >= threshold)
# Convert the locations from arrays to X/Y-coordinate tuples.
locations = list(zip(*locations[::-1]))
# Debug-print the locations to the console.
#print(locations)
rectangles = []
# For all the locations found...
for location in locations:
# Define a rectangle to draw around the location.
rectangle = [int(location[0]), int(location[1]), needle_width, needle_height]
# Append the rectangle to the rectangle collection.
rectangles.append(rectangle)
# Group duplicate rectangles from overlapping results.
rectangles, weights = cv.groupRectangles(rectangles, 1, 1)
# If we have any rectangles at all...
if len(rectangles):
print( Found needle(s)! )
for rectangle in rectangles:
# Determine the location position.
top_left = (rectangle[0], rectangle[1])
bottom_right = (rectangle[0] + rectangle[2], rectangle[1] + rectangle[3])
# Draw a transparent, filled rectangle over the detected needle image.
cv.rectangle(scribble_image, top_left, bottom_right, color = (0, 255, 0), thickness = -1)
result_image = cv.addWeighted(scribble_image, 0.25, haystack_image, 1 - 0.25, 0)
for rectangle in rectangles:
# Determine the location position.
top_left = (rectangle[0], rectangle[1])
bottom_right = (rectangle[0] + rectangle[2], rectangle[1] + rectangle[3])
# Draw a solid line around the detected needle image.
cv.rectangle(result_image, top_left, bottom_right, color = (0, 255, 0), thickness = 2, lineType = cv.LINE_4)
# Display the image in a window.
cv.imshow( Result , result_image)
else:
print( No needle found... )
# Pause the script.
cv.waitKey()
# Destroy all the OpenCV 2 windows.
cv.destroyAllWindows()
Images
Haystack image (sun_haven_14.jpg)
Needle image (sun_haven_beecube.jpg)
OpenCV script results (desired outcome)
Desired change
for rectangle in rectangles:
# Determine the location position.
top_left = (rectangle[0], rectangle[1])
bottom_right = (rectangle[0] + rectangle[2], rectangle[1] + rectangle[3])
# Draw a transparent, filled rectangle over the detected needle image.
cv.rectangle(scribble_image, top_left, bottom_right, color = (0, 255, 0), thickness = -1)
# Draw a solid line around the detected needle image.
cv.rectangle(scribble_image, top_left, bottom_right, color = (0, 255, 0), thickness = 2, lineType = cv.LINE_4)
result_image = cv.addWeighted(scribble_image, 0.25, haystack_image, 1 - 0.25, 0)
# Display the image in a window.
cv.imshow( Result , result_image)