简体   繁体   English

如何操作在opencv-python中使用minAreaRect()绘制的边界框内的像素

[英]How to manipulate the pixels inside a bounding box drawn using minAreaRect() in opencv - python

I have an image with few green bars. 我的图像有很少的绿色条。 But one of them are special because it's connected to a blue colored shape. 但其中一个是特殊的,因为它连接到蓝色的形状。 I want to draw a bounding box using minAreaRect() around the special green bar. 我想在特殊的绿色条形图周围使用minAreaRect()绘制一个边界框。

I was able to draw bounding boxes using minAreaRect() around all the green bars so far. 到目前为止,我能够在所有绿色条形图周围使用minAreaRect()绘制边界框。 But in order to filter the green bars and take only the special one, I need to identify which box contains the blue pixels. 但是为了过滤绿色条并仅采用特殊条,我需要确定哪个框包含蓝色像素。

In order to do that, I want to check every pixel inside every box to check which one contains blue pixels. 为了做到这一点,我想检查每个框内的每个像素,以检查哪个像素包含蓝色像素。 Is there any way to identify the pixel coordinates of the pixels inside a bounding box. 有没有办法识别边界框内像素的像素坐标。 Or is there a better approach ? 还是有更好的方法?

import cv2 as cv
import numpy as np

# Load the aerial image and convert to HSV colourspace
image = cv.imread("1.png")
image1 = image
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)

# Define lower and uppper limits of the color blue
low_blue = np.array([94, 80, 2])
high_blue = np.array([126, 255, 255])

# Mask image to only select blues
mask1 = cv.inRange(hsv, low_blue, high_blue)

# Change image to green where we found blue
image[mask1 > 0] = (0, 130, 0)


blurred_frame = cv.GaussianBlur(image, (5, 5), 0)
hsv = cv.cvtColor(blurred_frame, cv.COLOR_BGR2HSV)
low_green = np.array([25, 52, 72])
high_green = np.array([102, 255, 255])
mask = cv.inRange(hsv, low_green, high_green)
_, contours, _ = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
image[mask1 > 0] = (255, 0, 0)

for contour in contours:

    rect = cv.minAreaRect(contour)
    box = cv.boxPoints(rect)
    box = np.int0(box)
    Cx = rect[0][0]
    Cy = rect[0][1]

    cv.drawContours(image, [box], 0, (0, 0, 255), 2)

cv.imshow("Frame", image)
cv.waitKey(0)
cv.destroyAllWindows()

Here is the input image 这是输入图像

https://ibb.co/h9cv4DN https://ibb.co/h9cv4DN

Here is the expected output (bounding box indicated with purple color) 这是预期的输出(边框用紫色表示)

https://ibb.co/8Mq6Mwt https://ibb.co/8Mq6Mwt

This answer looks at all the green bars in the image. 此答案将查看图像中的所有绿色条。 It check if the green bar also contains a blue color. 它检查绿色条是否也包含蓝色。

for contour in contours:

    rect = cv.minAreaRect(contour)
    box = cv.boxPoints(rect)
    box = np.int0(box)
    Cx = rect[0][0]
    Cy = rect[0][1]

    # Make a mask of this single green line
    mask = np.zeros_like(mask1)
    cv.drawContours(mask, [contour], 0, 255, cv.FILLED)
    sigle_green_line = cv.bitwise_and(image, image, mask = mask)
    sigle_green_line = cv.cvtColor(sigle_green_line, cv.COLOR_BGR2HSV)
    # Check how much blue is in the image
    blue_mask = cv.inRange(sigle_green_line, low_blue, high_blue)
    print(sum(sum(blue_mask)))
    # If the image is not all black (all zeros) the contour contains some blue
    if sum(sum(blue_mask)) > 0: print('This contour contains some blue')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 openCV 中“拉伸”出从 minAreaRect function 给出的边界框? - How to "stretch" out a bounding box given from minAreaRect function in openCV? 找到在opencv(python)中设置的白色像素的边界框 - find the bounding box of a white pixels set in opencv (python) 使用Python OpenCV,如何在特定颜色边界框内提取图像区域? - Using Python OpenCV, How would you extract an image area inside a particular color bounding box? 如何使用openCV或Python从图像中删除黑色边框 - How to remove black bounding box from the image using openCV or Python 如何在手动绘制的边框内提取图像 - How to extract image inside bounding box which was drawn manually Opencv:如何在python中绘制一个旋转的边界框 - Opencv: how to draw a rotated bounding box in python 使用带有KNN算法的OpenCV Python检测面部丘疹的边界框 - Bounding Box on Detecting pimples on face using OpenCV Python with KNN algorithm Python文件使用OpenCV写入所有边界框坐标 - Python file write all the bounding box coordinates using OpenCV 如何绘制更大的边界框和仅裁剪边界框文本 Python Opencv - How to draw Bigger bounding box and Crop Only bounding box Text Python Opencv 如何在每条线上绘制单个边界框,裁剪边界框并将图像保存在 opencv python 文件夹中 - How to draw a single bounding box on each line ,crop the bounding box and save image in folder opencv python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM