从两个旅中获取平均元素
如您在 来自明亮的蓝色轮廓有两种电子。我试图通过将轮廓阵列合并为一个阵列来获得“卑鄙” - elipse。结果是蓝线。那不是我去的。有人可以向我解释我如何归档 。我希望绿线作为弹性。 向所有人致敬。
这是代码的一部分,
image_read = cv2.imread(file)
image_read_gray = cv2.cvtColor(image_read, cv2.COLOR_BGR2GRAY)
image_copy = image_read_gray.copy()
kernel = np.ones((3, 3), np.uint8)
closing = cv2.morphologyEx(image_copy, cv2.MORPH_CLOSE, kernel)
#cv2.imshow("blabla",closing)
#cv2.waitKey(0)
image_read2 = cv2.imread(path_folder_croppedoriginal + "\\image_cropped" + str(count) + ".jpg")
contours, hierarchy = cv2.findContours(closing, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
sorted_contour = sorted(contours, key=cv2.contourArea, reverse=True)
bigcontour1 = sorted_contour[0]
bigcontour2 = sorted_contour[1]
bigcontour_merged = np.concatenate((bigcontour1,bigcontour2))
ellipse = cv2.fitEllipse(bigcontour1)
ellipse2 = cv2.fitEllipse(bigcontour2)
ellipse3 = cv2.fitEllipse(bigcontour_merged)
image_read_BGR = cv2.cvtColor(image_read_gray, cv2.COLOR_GRAY2BGR)
ImgWithEllipse = cv2.ellipse(image_read_BGR, ellipse, (255, 0 , 255), 4, cv2.LINE_AA)
cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithEllipse)
ImgWithEllipse2 = cv2.ellipse(image_read_BGR, ellipse2, (255, 0, 255), 4, cv2.LINE_AA)
cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithEllipse2)
ImgWithEllipse_merged = cv2.ellipse(image_read_BGR, ellipse3, (255, 0, 0), 4, cv2.LINE_AA)
cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithEllipse_merged)
ImgWithCounter2 = cv2.drawContours(image_read2, [bigcontour1, bigcontour2], -1, (255, 255, 0), 3)
cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithCounter2)
ImgWithCounter = cv2.drawContours(image_read_BGR, [bigcontour1,bigcontour2], -1, (255, 255, 0), 3)
print(cv2.contourArea(bigcontour1), cv2.contourArea(bigcontour2))
cv2.imwrite(os.path.join(path_folder_contours, "image_contours" + str(count) + ".jpg"), ImgWithCounter)
count += 1
也许是BC的一部分,外元素(例如1900点表示)比内元素(例如600)还有更多点。
As you can see in the there are two elipses from the bright blue contours. I tried to get the "mean" - elipse by merging the contour arrays into one array. The result is the blue line. Thats not what i exspected. Can somebody explain me how i can archive
. I want the green line as a elipse.
Thx to everyone.
Here is part of the code
image_read = cv2.imread(file)
image_read_gray = cv2.cvtColor(image_read, cv2.COLOR_BGR2GRAY)
image_copy = image_read_gray.copy()
kernel = np.ones((3, 3), np.uint8)
closing = cv2.morphologyEx(image_copy, cv2.MORPH_CLOSE, kernel)
#cv2.imshow("blabla",closing)
#cv2.waitKey(0)
image_read2 = cv2.imread(path_folder_croppedoriginal + "\\image_cropped" + str(count) + ".jpg")
contours, hierarchy = cv2.findContours(closing, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
sorted_contour = sorted(contours, key=cv2.contourArea, reverse=True)
bigcontour1 = sorted_contour[0]
bigcontour2 = sorted_contour[1]
bigcontour_merged = np.concatenate((bigcontour1,bigcontour2))
ellipse = cv2.fitEllipse(bigcontour1)
ellipse2 = cv2.fitEllipse(bigcontour2)
ellipse3 = cv2.fitEllipse(bigcontour_merged)
image_read_BGR = cv2.cvtColor(image_read_gray, cv2.COLOR_GRAY2BGR)
ImgWithEllipse = cv2.ellipse(image_read_BGR, ellipse, (255, 0 , 255), 4, cv2.LINE_AA)
cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithEllipse)
ImgWithEllipse2 = cv2.ellipse(image_read_BGR, ellipse2, (255, 0, 255), 4, cv2.LINE_AA)
cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithEllipse2)
ImgWithEllipse_merged = cv2.ellipse(image_read_BGR, ellipse3, (255, 0, 0), 4, cv2.LINE_AA)
cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithEllipse_merged)
ImgWithCounter2 = cv2.drawContours(image_read2, [bigcontour1, bigcontour2], -1, (255, 255, 0), 3)
cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithCounter2)
ImgWithCounter = cv2.drawContours(image_read_BGR, [bigcontour1,bigcontour2], -1, (255, 255, 0), 3)
print(cv2.contourArea(bigcontour1), cv2.contourArea(bigcontour2))
cv2.imwrite(os.path.join(path_folder_contours, "image_contours" + str(count) + ".jpg"), ImgWithCounter)
count += 1
Maybe its bc there are more points for the outer elipse (like 1900 points to represent) then for the inner elipse(like 600)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论