简体   繁体   English

在轮廓和扩展轮廓之间反转像素颜色

[英]Invert pixel color between a contour and the extended contour

In am image, I am finding contours and then picking one contour at a time and then modifying each one of them. 在图像中,我正在寻找轮廓,然后一次选择一个轮廓,然后修改其中的每个轮廓。 I need to invert colors of the region only between those two contour lines. 我只需要反转两条轮廓线之间的区域颜色。

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.

在此处输入图片说明

在此处输入图片说明

Expected output: 预期产量:

The expected is the entire image with the pixels inverted between the two contours. 期望的是整个图像,其中像素在两个轮廓之间反转。

Binarize your image and invert it by applying a bitwise not operator 通过应用按位非运算符对图像进行二值化处理并将其反转

im = np.clip(im,0,255).astype(np.uint8)
im = cv2.bitwise_not(im)

or: 要么:

_, im = cv2.threshold(im.astype(np.uint8),0,255,cv2.THRESH_OTSU|cv2.THRESH_BINARY_INV)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM