在图像中,我正在寻找轮廓,然后一次选择一个轮廓,然后修改其中的每个轮廓。我只需要反转两条轮廓线之间的区域颜色。
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
%matplotlib inline
im = cv2.imread('bigO.jpg')
im = np.invert(im)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
# plt.figure(figsize=(10,10))
# plt.imshow(imgray)
ret,thresh = cv2.threshold(imgray, 127,255,cv2.THRESH_BINARY)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#Get any one contour
cnt = contours[1]
#Extend the contour
def rand_xy(mu, sigma):
return np.random.normal(mu, sigma), np.random.normal(mu, sigma)
cnt_new = np.asarray([point + rand_xy(mu = 5., sigma = 0.5) for point in cnt], dtype=np.int32)
#DRAW CONTOURS
cv2.drawContours(im, cnt, -1, (255, 0, 0), 1)
cv2.drawContours(im, cnt_new, -1, (255, 0, 0), 1)
plt.figure(figsize=(10,10))
plt.imshow(im)
#Here I need to invert the color of images between the contours.
预期输出:
期望的是整个图像,像素在两个轮廓之间反转。
答案 0 :(得分:0)
对图像进行二值化处理,然后应用按位非运算符
im = np.clip(im,0,255).astype(np.uint8)
im = cv2.bitwise_not(im)
或:
_, im = cv2.threshold(im.astype(np.uint8),0,255,cv2.THRESH_OTSU|cv2.THRESH_BINARY_INV)