簡體   English   中英

對圖像的特定部分應用圖像處理 Python OpenCV

[英]Apply Image Processing on Specific part of the image Python OpenCV

我想在單個圖像的不同部分應用不同類型的閾值。 有什么方法可以對圖像的特定部分而不是整個圖像應用閾值。 以下是適用於完整圖像的我的代碼。 如何修改? 導入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) 這個坐標范圍的閾值。

你在找這樣的東西嗎?

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()

示例 output:

在此處輸入圖像描述

相似答案:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM