English 中文(简体)
露天焚烧场: 在一个 lo体内填充和中风
原标题:OpenCV in Python: Rectangle with fill and stroke in one loop

OpenCV rectangle fill & stroke in a single loop

Introduction

Hi! 我试图在开放式CV和Av的模版配对上添加一条斜体,并用透光填充和坚固的中风。 从事这项工作的材料Im来自一个名为:Sun Haven ,由Pixel Sprout生产商在

Setup

<<>Operating System: 微软Windows 11

<>Python>:3.10.4

<><0>>>>>

Problem

目前,我有一份将作trick的文字,但我不喜欢它的执行方式,因为我现在不得不两次通过模板结果:

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)

“Sun

Needle image (sun_haven_beecube.jpg)

Beecube from Sun Haven. Credits: Pixel Sprout Studios

OpenCV script results (desired outcome)

“预期结果,但代码低劣

如你所看到的那样,这发现并凸显了原形象中的四种贝.,但我却把两种环状混为一谈,不重复直径和性能。

Desired change

我试图将这两处 lo合成一:

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)

However, this does not yield the results I m looking for, as it will only draw the transparent fill, not the stroke: Undesired results with fill, but no 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!

最佳回答
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签