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.
I was able to draw bounding boxes using minAreaRect() around all the green bars so far. 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
Here is the expected output (bounding box indicated with purple color)
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.