[質問]
画像に関する処理の関数化について学んでいます。
下記30行目にてsyntaxerrorが発生してしまいました。
理由がわからないのでアドバイスが欲しいです。
[エラーメッセージ]
if (data[i][4] > loArea) & (data[i][4] < hiArea): ↑
[コード]
import cv2 from PIL import Image from matplotlib import pyplot as plt import numpy as np def pimanDetect(img): thresh = 110 kernel1 = np.ones((15,15),np.uint8) kernel2 = np.ones((10,10),np.uint8) loArea = 20000 hiArea = 40000 img_src = np.copy(img) img_b, img_g, img_r = cv2.split(img) img_g = cv2.GaussianBlur(img_g, (15, 15), 2) ret, img_thresh = cv2.threshold(img_g, thresh, 255, cv2.THRESH_BINARY) img_dilation = cv2.dilate(img_thresh,kernel1,iterations = 1) img_erosion = cv2.erode(img_dilation,kernel2,iterations = 1) label = cv2.connectedComponentsWithStats(img_erosion) n = label[0] - 1 data = np.delete(label[2], 0, 0) center = np.delete(label[3], 0, 0) count = 1 for i in range(n): if (data[i][4] > loArea) & (data[i][4] < hiArea): x0 = data[i][0] y0 = data[i][1] x1 = data[i][0] + data[i][2] y1 = data[i][1] + data[i][3] cv2.rectangle(img_src, (x0, y0), (x1, y1), (0, 0, 255),thickness=5) cv2.putText(img_src, "No: " +str(count), (x1, y1 - 85), cv2.FONT_HERSHEY_PLAIN, 5, (0, 255, 255),4) cv2.putText(img_src, "Size: " +str(data[i][4]), (x1, y1), cv2.FONT_HERSHEY_PLAIN, 5, (0, 255, 255),4) count=count+1 return img_src
よろしくお願いします。
出典のURLなどを載せてください。
回答2件
あなたの回答
tips
プレビュー