我很难让我的代码找到我创建的第一个图像后创建的轮廓。这部分程序的目标是首先创建一个图像,如:
然后使用color_separator函数将其分解为以下图像。这样做的目的是将每个单独的图像按颜色分开,以便从上面的图像中,我们可以得到:
然后,我尝试使用OpenCV中的基本findContours来查找轮廓。问题在于findContours部分:它将找到第一组轮廓;但对于其他轮廓,轮廓是空白的。图像加载正确;所有操作都正常,直到第一个之后的所有图像都找到了目录。例如,绿色图片会找到它的轮廓,而不会打印“无轮廓”检查语句;但其余图像将打印“无轮廓”检查语句。如果有人能帮忙,我会非常感激的。在
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
问题是图像上的颜色在转换成灰度时太暗了。绿色的还不错,但深蓝一点也不能很好地转换成灰度。故事的寓意:当从RGB转换为灰度时,请确保您的颜色足够轻,可以进行转换。在
相关问题 更多 >
编程相关推荐