opencv 使用python,试图通过裁剪出最亮点来找到图像最亮点周围的颜色范围

oogrdqng  于 2023-06-24  发布在  Python
关注(0)|答案(1)|浏览(122)

我有一个程序,检测,然后圈出最亮的部分的图像。我需要能够获得圆圈区域内的颜色值范围,我正在考虑通过裁剪圆圈区域来实现这一点,然后获得裁剪图像中所有内容的像素颜色值。
但我不知道如何裁剪出部分图像的基础上,最亮的点被发现。
下面是我目前为止得到的代码

import cv2
import numpy as np
img1 = cv2.imread('opencv_frame_0.png')
img2 = cv2.imread('opencv_frame_1.png')
vis = np.concatenate((img1, img2), axis=1)
cv2.imwrite('combined.png', vis)

# load the image and convert it to grayscale
#image = cv2.imread(args["image"])
image = cv2.imread('combined.png')
orig = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#perform a naive attempt to find the (x, y) coordinates of the area of the image with the largest intensity value
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)

# display the results of the naive attempt
cv2.imshow("Naive", image)
#apply a Gaussian blur to the image then find the brightest region
#gray = cv2.GaussianBlur(gray, (args["radius"], args["radius"]), 0)
gray = cv2.GaussianBlur(gray, (41, 41), 0)
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
image = orig.copy()
#cv2.circle(image, maxLoc, args["radius"], (255, 0, 255), 2)
cv2.circle(image, maxLoc, 41, (255, 0, 255), 2)
#display the results of our newly improved method
cv2.imshow("Robust", image)
cv2.waitKey(0)

下面是我试图裁剪出不起作用的明亮区域的方法之一

import numpy as np

import cv2

# load the image and convert it to grayscale
image = cv2.imread('combined.png')
image1 = cv2.imread('combined.png', 0)
orig = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#perform a naive attempt to find the (x, y) coordinates of the area of the image with the largest intensity value
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)

# display the results of the naive attempt
#cv2.imshow("Naive", image)

#apply a Gaussian blur to the image then find the brightest region
gray = cv2.GaussianBlur(gray, (41, 41), 0)
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
image = orig.copy()
cv2.circle(image, maxLoc, 41, (255, 0, 255), 2)


#experimenting with cropping out the circle

height,width = image1.shape
mask = np.zeros((height,width), np.uint8)

circle_img = cv2.circle(mask,(maxLoc,(255,255,255))

masked_data = cv2.bitwise_and(image, image, mask=circle_img)

_,thresh = cv2.threshold(mask,1,255,cv2.THRESH_BINARY)

contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
x,y,w,h = cv2.boundingRect(contours[0])

crop = masked_data[y:y+h,x:x+w]

cv2.waitKey(0)
gojuced7

gojuced71#

你不需要剪去圈出的区域来得到颜色范围。可以将圆形遮罩应用于minMaxLoc。假设你有图像的红色通道,那么你可以直接提取红色范围:

mask = np.zeros((height, width), np.uint8)
circle_img = cv2.circle(mask, maxLoc, r, 255, cv2.FILLED)
minRed, maxRed, minLocRed, maxLocRed = cv.minMaxLoc(image_channel_red, mask=circle_img)

相关问题