对图像的特定部分应用图像处理Python OpenCV

yptwkmov  于 2023-01-26  发布在  Python
关注(0)|答案(1)|浏览(196)

我想应用不同类型的阈值在一个单一的图像,但它的不同部分。有没有办法应用阈值在图像的特定部分,而不是在整个图像。下面是我的代码,适用于整个图像。如何修改它?导入cv2

image = cv2.imread('ANNOTATION01_monitor.PNG')
cv2.imshow('Original',image)
cv2.waitKey()
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale', grayscale)
cv2.waitKey()
ret,thresh1 = cv2.threshold(after_img,30,255,cv2.THRESH_BINARY)
cv2.imshow('Thresholding', thresh1)
cv2.waitKey()

它对整幅图像进行阈值处理,我想在这个坐标范围内,从(x1,y1)到(x2,y2)进行阈值处理。

l7wslrjt

l7wslrjt1#

你在找这样的东西吗?

import cv2

x1, y1, x2, y2 = 20, 20, 200, 160

img = cv2.imread('img.png')

roi = img[y1:y2, x1:x2]                                            # get region
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)                       # convert to gray
ret, thresh = cv2.threshold(gray, 64, 255, cv2.THRESH_BINARY)      # compute threshold
bgr = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)                     # convert to bgr
img[y1:y2, x1:x2] = bgr                                            # paste into region

cv2.imshow('output', img)
cv2.waitKey()
cv2.destroyAllWindows()

输出示例:

相似答案:

相关问题