我的第一个问题是,我想在我剪切的图像中逐个像素地查看并计算颜色值,但我得到了以下错误:
属性错误:"numpy. ndarray"对象没有属性"width"
我的第二个问题是,例如,当match_masking_color值为255时,该区域变为蓝色,即使我设置阈值并将其转换为二进制图像,它仍保持蓝色。例如,我输入了值255,255,255,但这一次嘴的内部是红色的。我希望嘴是黑色的,脸是白色的,我可以数出这两种颜色的数量。下面是我所说的结果:blue mask1blue mask2red mouth
import cv2
import numpy as np
img = cv2.imread("saskin.jpg")
black_count = 0
white_count = 0
def region_of_interest(image, vertices):
mask = np.zeros_like(img)
match_mask_color = 255,255,255
cv2.fillPoly(mask, vertices, match_mask_color)
masked_image = cv2.bitwise_and(img, mask)
return masked_image
while True:
# ret, frame = cap.read()
# frame = cv2.flip(frame,1)
# print(frame. Shape) # 480,640
ycrbc = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
minYCrCb = np.array([0,140,90],np.uint8)
maxYCrCb = np.array([230,170,120],np.uint8)
imgeYCrCb = cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
skinRegionYCrCb = cv2.inRange(imgeYCrCb,minYCrCb,maxYCrCb)
skinYCrCb = cv2.bitwise_and(img, img, mask = skinRegionYCrCb)
median_ycrcb = cv2.medianBlur(skinYCrCb, 3)
_, esik = cv2.threshold(median_ycrcb, 20, 255, cv2.THRESH_BINARY)
median_binary = cv2.medianBlur(esik, 7)
gray = cv2.cvtColor(median_binary, cv2.COLOR_BGR2GRAY)
contours, hierarchy = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
x,y,w,h = cv2.boundingRect(contours[max_index])
height, width = h, w
region_of_interest_vertices = [(width/4, 11*height/12), (width/4, 13*height/16), (3*width/4, 13*height/16), (3*width/4, 11*height/12)]
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cropped_image = region_of_interest(gray_image, np.array([region_of_interest_vertices], np.int32))
_, agiz = cv2.threshold(cropped_image, 20, 255, cv2.THRESH_BINARY)
for x in range(agiz.width):
for y in range(agiz.height):
pixel = img.getpixel((x, y))
if pixel == (0):
black_count += 1
elif pixel == (255):
white_count += 1
cv2.imshow("ycrbc",ycrbc)
cv2.imshow("skinYCrCb",median_ycrcb)
cv2.imshow("binary goruntu", esik)
cv2.imshow("median_binary", median_binary)
cv2.imshow("Kesilmiş Görüntü",cropped_image)
cv2.imshow("agiz", agiz)
if cv2.waitKey(5) & 0xFF == ord("q"):
break
# cap.release()
cv2.destroyAllWindows()
我怎样才能使输出由黑色和白色组成,作为我想要的蒙版的结果?我怎样才能在裁剪图像的像素值内导航?
1条答案
按热度按时间jexiocij1#
看起来你混淆了两个不同的包,cv2和PIL。
width
,height
和getpixel
属于PIL的Image.image示例,而你使用cv2,其中图像被表示为numpy数组。因此你需要通过agiz.shape
访问图像的维度:关于您的错误消息:将来发布错误消息的完整追溯会更有帮助,因为它直接指出了错误发生的位置。