区域像素可以进行处理,我做了一个鼠标选取两点,将这块区域置零的操作。
import cv2
import numpy as np
img = cv2.imread('C:\\Users\\mimi\\Desktop\\1.jpg')
a =[]
b = []
def on_EVENT_LBUTTONDOWN(event, x, y,flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
xy = "%d,%d" % (x, y)
a.append(x)
b.append(y)
cv2.circle(img, (x, y), 1, (255, 0, 0), thickness=-1)
cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
1.0, (0, 0, 0), thickness=1)
cv2.imshow("image", img)
cv2.namedWindow("image")
cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
cv2.imshow("image", img)
cv2.waitKey(0)
print(a[0],b[0])
img[b[0]:b[1],a[0]:a[1],:] = 0 #注意是 行,列(y轴的,X轴)
cv2.imshow("image", img)
cv2.waitKey(0)
print (a,b)
不能对图像的像素处理,我做了一个区域置黑的操作没成功,上一个代码可以实现
import matplotlib.pyplot as plt
from PIL import Image
im = Image.open("C:\\Users\\mimi\\Desktop\\1.jpg")
plt.imshow(im, cmap = plt.get_cmap("gray"))
pos=plt.ginput(2)
print(pos)
a=[]
b=[]
for i in range(len(pos)):
a.append(pos[i][0])
b.append(pos[i][1])
print (a,b)
im[a[0]:a[1],b[0]:b[1],:]
plt.imshow(im)