简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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