python - 我如何获得opencv中所有对象的所有像素?
问题描述
我有带有单独对象的图像,这些对象具有一种颜色。
示例图像:
我想从每个对象中获取所有像素。我使用 Python 和 CV2。但我不知道该怎么做。
我想得到的例子:
- object1 : [(x1,y1), (x2,y2) ... (xn1,yn1)] 其中 n1 - 计算 object1 中的所有像素
- object2 : [(x1,y1), (x2,y2) ... (xn2,yn2)] 其中 n2 - 计算 object2 中的所有像素
- ...
- objectm : [(x1,y1), (x2,y2) ... (xnm,ynm)] 其中 nm - 计算 objectm 中的所有像素
UPD:这可以通过 cv2.connectedComponents() 来完成。在 python 中查看这个连接的组件标签。谢谢烧杯
解决方案
考虑到 img 在 'img' 中打开,您可以使用 immg[i,j] 返回蓝色红色绿色的值,例如
>>img[12,12]
[143,144,255] // this is what is returned [blue green red]
所以你可以使用类似的东西
rows = img.shape[0]
cols = img.shape[1]
for (i in range(0,rows)):
for (j in range(0,cols)):
bgr=img[i,j]
#now use if condition and match brg values with color you wnana detect then append the pair i,j in the a list if the condition matcches
推荐阅读
- php - 在 Laravel 中发送消息
- docker - stripe-cli 总是返回 401
- c++ - 平方模型为零,阶乘模型问题
- php - 如何强制 PHP 方法中的 curl 请求失败以进行单元测试
- sql-server - Artifactory 可以使用具有不同数据库和数据库用户名的 SQL Server 数据库吗?
- python - 在pySpark中将YYYYMMDD的字符串转换为DD.MM.YYYY的日期
- reactjs - 使用秘密 API 令牌将反应应用程序部署到 Netlify
- python - pygame.event.get() 中的事件:妨碍了 Timer
- flutter - 在颤动中使用字符串的日期时间月份
- apify - Apify Puppeteer Scraper 错误:actor 运行已达到超时