计算图像中不规则形状的对象覆盖的面积 [英] Calculating the area covered by the objects of irregular shapes in an image

查看:55
本文介绍了计算图像中不规则形状的对象覆盖的面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我根据图像中的小麦峰值估算小麦单产.使用Faster-RCNN和基于颜色的分割检测出峰值之后,下面是所得图像,其中图像中只有峰值.

I am working on a project where I'm estimating the wheat yield based on the wheat spikes in the image. After detecting spikes using Faster-RCNN and color based segmentation, the following is the resultant image where there are only spikes in the image.

现在,我的目标是使用python估计图像中尖峰产生的产量.为此,我们可能必须计算出多边形形状的对象所覆盖的面积,或者可能必须围绕像素值来计算面积.但是我不知道我们该怎么做.请让我知道是否有人可以解决.谢谢

Now my goal is to estimate the yield produced by the spikes in the image using python. For this, we may have to calculate the area covered by the objects of polygon shapes or we may have to work around the pixel values to calculate the area. But I don't know how we can do this. Please Let me know If anyone has the solution. Thanks

推荐答案

可以通过创建二进制蒙版找到非黑色图像像素区域.以像素为单位的面积等于蒙版中白色像素的总数.一种获取方法是计算图像中白色像素的比例.白色像素的数量将是图像的分数*宽度*高度.分数只是图像的平均值除以最大可能的灰度等级(255).所以

The area in pixels of the image that are not black can be found from creating a binary mask. The area in pixels is equal to the total number of white pixels in the mask. One way to get that is to compute the fraction of white pixels in the image. The number of white pixels will then be the fraction * width * height of the image. The fraction is just the average of the image divided by the maximum possible gray level (255). So

以白色像素为单位的像素面积=(平均值/255)宽度高度

area in pixels of white pixels = (average/255)widthheight

因此,获得二进制蒙版图像的分数平均值(平均值/255)(通过将阈值设置为0).平均值的结果将是一个单一值.然后将其乘以图像的宽度,再乘以图像的高度.该结果将等于蒙版中白色像素的总数,因此等于图像中非黑色(即彩色)的像素总数.白色像素数是图像中非黑色像素的像素面积.

Thus, get the fractional average (average/255) of the binary mask image (by thresholding at 0). The result for the average will be one single value. Then multiply that by the Width of the image and then by the Height of the image. That result will be equal to the total number of white pixels in the mask and thus the total pixels that are not black (i.e. are colored) in your image. The number of white pixels is the pixel area of the non-black pixels in your image.

输入:

import cv2
import numpy as np
img = cv2.imread('img.jpg')
height = img.shape[0]
width = img.shape[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY)
cv2.imshow("Mask", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
ave = cv2.mean(thresh)[0]/255
print(ave)
0.310184375
area = ave*height*width
print(area)
198518.0

请注意,这是非黑色像素区域.您的某些矩形内部有黑色.因此,这不是矩形的区域.在隔离矩形以获取矩形区域之前,您将确保图像没有黑色像素.

Note that this is the non-black pixel area. Some of your rectangles have black inside them. So this is not the area of the rectangle. You would have ensure that your image had no black pixels before isolating the rectangles to get the area of the rectangles.

添加

Mark Setchell建议的一种更简单的方法是简单地计算阈值图像中非零像素的数量.它计算出与上述相同的数字.

A simpler approach, suggested by Mark Setchell, is to simply count the number of nonzero pixels in the thresholded image. It computes the same number as above.

import cv2
import numpy as np
img = cv2.imread('img.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY)
cv2.imshow("Mask", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
area2 = cv2.countNonZero(thresh)
print(area2)
198518

添加2

如果您知道与该图像所覆盖的区域相对应的地面面积或尺寸(以米为单位,则留有0.8 m根据您的评论),则与非零像素计数相对应的地面面积将为:

If you know the ground area or dimensions in meters (0.8 m on aside as per your comment) corresponding to the area covered by the image, then the ground area corresponding to the count of non-zero pixels will be:

area on ground for nonzero pixels = count * 0.8 * 0.8 / (width * height)


其中宽度和高度是图像的像素尺寸.


where width and height are the pixel dimensions of the image.

import cv2
import numpy as np
img = cv2.imread('img.jpg')
height = img.shape[0]
width = img.shape[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY)
cv2.imshow("Mask", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
count = cv2.countNonZero(thresh)
area = count*0.8*0.8/(width*height)
print(area)
0.19851800000000003


结果是0.198518平方米


So the result is 0.198518 square meters

这篇关于计算图像中不规则形状的对象覆盖的面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆