使用 opencv 提取最大的斑点
Extract biggest blob with opencv
我有一个二进制图像,将人类描绘成白色斑点,背景描绘成黑色。我想使用 opencv 从大图像中"裁剪"最大(或 3 个最大(斑点。
怎么做呢?
我不确定您是否找到了答案,但根据我所理解的您的要求,这是代码的基本结构。您可以根据需要对其进行修改。
import numpy as np
import cv2
# load the image
image = cv2.imread("path_to_your_image.png") # if this not a binary image, you can threshold it
output = image.copy()
im2,contours,hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if len(contours) != 0:
# the contours are drawn here
cv2.drawContours(output, contours, -1, 255, 3)
#find the biggest area of the contour
c = max(contours, key = cv2.contourArea)
x,y,w,h = cv2.boundingRect(c)
# draw the 'human' contour (in green)
cv2.rectangle(output,(x,y),(x+w,y+h),(0,255,0),2)
# show the image
cv2.imshow("Result", output)
cv2.waitKey(0)
注意:x,y,x+w和y+h为您提供了框的范围,因此有了这些值,您可以获得最大斑点的感兴趣区域。
希望这有帮助!
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- OpenCV C++.快速计算混淆矩阵
- 在C++代码中包含opencv时,使用ctypes创建.so文件
- 哪些库可以通过Opencv调整曝光率
- 安装opencv失败-粘贴CMakeError.log的内容
- C++中的openCV Mat访问冲突
- OpenCV Android C++ imwrite not found
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- CV_OCL_RUN宏如何在OpenCV(版本3.4.5)的goodFeaturesToTrack实现中工作?
- openCV C++将对象/斑点简化为质心
- 使用 opencv 提取最大的斑点
- OpenCV 检测带有掩码的斑点
- OpenCV去除边缘斑点
- 具有不同投资回报率的扩张梯度(斑点优化)OPENCV