我无法获取代码以查找在创建第一个图像后创建的图像的轮廓 . 该部分程序的目标是首先创建一个如下图像:
然后使用color_separator函数将其分解为如下图像 . 这样做是按颜色分隔每个单独的图像,以便从上面的图像,我们得到:
然后,我尝试在OpenCV中使用基本的findContours来查找轮廓 . 问题出在findContours部分:它会找到第一组轮廓;但对于其他人来说,轮廓是空白的 . 图像加载正确;一切都工作,直到findContours为所有图像后的第一个 . 举个例子,绿色图片会找到它的轮廓,而不是打印“No Contours”检查语句;但其余图像将打印“无轮廓”检查语句 . 如果有人可以提供帮助,我真的很感激 .
def getAttributesFromNetwork(self,Network,image):
ShapeList = []
AngleList = []
FillList = []
SizeList = []
print Network.letter
# Open the image path from the Problems (Image Data) folder
image = Image.open(image)
grayscale = image.convert("L")
blackwhite = grayscale.point(self.filter,"1")
image = blackwhite
image = image.convert("RGB")
width,height = image.size
colorindex = 0
# Translate the image into pictures with various colors
while True:
color = DISTINCT_COLORS[colorindex]
colorindex += 1
blackpixel = None
for x,y,pixel in self.walk(image):
if pixel == (0,0,0):
blackpixel = (x,y)
break
if not blackpixel:
break
neighbors = [blackpixel]
while len(neighbors) > 0:
processing = list(neighbors)
neighbors = []
for x,y in processing:
image.putpixel((x,y),color)
new = [(x-1,y),(x+1,y),(x,y-1),(x,y+1)]
for x,y, in new:
if (x,y) in neighbors:
continue
if x < 0 or x >= width:
continue
if y < 0 or y >= height:
continue
if image.getpixel((x,y)) != (0,0,0):
continue
neighbors.append((x,y))
# We use the count to save each network as a different image
self.count = str(self.count)
# Save the network image
image.save("colored"+self.count+".png")
# Open the network image; here, we'll convert it to a bunch of different
# images; each with a different shape
im = Image.open("colored"+self.count+".png")
# Separate the images
colors_dict = color_separator(im)
#print colors_dict
# show the images:
imageCount = 0
# Iterate through the color dictionary for all of the images
for key,value in colors_dict.iteritems():
if key == (255, 255, 255):
imageCount += 1
continue
imageCount = str(imageCount)
# grab the individual image,
image = value
# save it,
image.save(Network.letter+"coloredSmall"+imageCount+".png")
# then read it back with OpenCV for processing
img = cv2.imread(Network.letter+"coloredSmall"+imageCount+".png")
# Convert it to grayscale; it processes better this way
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
# find the contours in the image
contours,hierarchy = cv2.findContours(thresh,1,2)
#count = 0
# iterate through the contours,
if not contours:
print "No Contours"
for cnt in contours:
print "Looking through contours"
#if (count%2) == 1:
#count = count + 1
#print "Count2: ",count
#continue
# approximate how many sides it has
approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
print len(approx)
if len(approx) == 5:
print "Half-Arrow"
ShapeList.append("Half-Arrow")
if len(approx) == 7:
print "Arrow"
ShapeList.append("Arrow")
elif len(approx) == 3:
print "Triangle"
ShapeList.append("Triangle")
elif len(approx) == 4:
print "Square"
ShapeList.append("Square")
elif len(approx) >= 13:
print "Circle"
ShapeList.append("Circle")
elif len(approx) == 12:
print "Cross"
ShapeList.append("Cross")
(x,y),(MA,ma),angle = cv2.fitEllipse(cnt)
AngleList.append(angle)
#count = count + 1
#print "Count: ",count3
print ShapeList
cv2.imshow("img",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
imageCount = int(imageCount)
imageCount += 1
1 回答
问题是当转换为灰度时图像上的颜色太暗 . 绿色的很好;但是深蓝色根本没有很好地转换成灰度 . 故事的道德:当从RGB转换为灰度时,请确保您的颜色足够轻以便转换 .