我正在尝试将最小边界框适合下面显示的每个“斑点”。作为图像处理管道的一部分,我使用 findContours 来检测数据中的轮廓,然后在给定一组已发现的轮廓的情况下绘制最小边界框。
最小边界框不是很准确——一些特征显然被遗漏了,而另一些特征则不能完全“封装”一个全连接的特征(而是被分割成几个小的最小边界框)。我玩过检索模式(如下所示的 RETR_TREE)和轮廓近似方法(如下所示的 CHAIN_APPROX_TC89_L1),但找不到我真正喜欢的东西。有人可以建议一种更强大的策略来使用 OpenCV Python 更准确地捕获这些轮廓吗?

import numpy as np
import cv2
# load image from series of frames
for x in range(1, 20):
convolved = cv2.imread(x.jpg)
original = convolved.copy
#convert to grayscale
gray = cv2.cvtColor(convolved, cv2.COLOR_BGR2GRAY)
#find all contours in given frame, store in array
contours, hierarchy = cv2.findContours(gray,cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_L1)
boxArea = []
#draw minimum bounding box around each discovered contour
for cnt in contours:
area = cv2.contourArea(cnt)
if area > 2 …Run Code Online (Sandbox Code Playgroud) python opencv computer-vision image-segmentation opencv-contour
我一直在尝试找到弯曲形状(例如香蕉)的中心。我可以完成所有基础操作,例如创建二值图像和定位轮廓。但是,质心函数正确地找到了轮廓之外的点。我需要的点必须在轮廓内。我附上了一张应该能更好地解释事情的图片。
如果有人有任何想法,或看到过类似的东西,我将非常感谢您的帮助。
我试图在一个二进制图像中找到轮廓,这是一个 numpy 数组
a = np.array(np.random.rand(1024,768),dtype='float32')
_, t2 = cv2.threshold(a,127,255,0)
im2, contours, hierarchy = cv2.findContours(t2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)
当我尝试运行该代码时,出现此错误
OpenCV Error: Unsupported format or combination of formats
([Start]FindContours supports only CV_8UC1 images when
mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only)
in cvStartFindContours
Run Code Online (Sandbox Code Playgroud) 假设如果我们正在处理图像,是否有任何方法可以访问轮廓内的像素?
我已经使用函数findContours()找到了轮廓,甚至找到了瞬间,但找不到轮廓内的像素。
任何建议都欢迎!!
谢谢!
我已经能够制作自适应阈值并检测旋转角度(我不确定是否必须旋转图像)
我正在努力检测包含表单的矩形。我尝试了不同的方法,例如 opencv 的 findContours()。它能够找到的最大轮廓是一个带有名字的框。
之后我决定使用HoughLinesP,但它发现了很多行,我不知道如何过滤它们。检测矩形以校正表格也很方便,之后我将能够轻松阅读答案。所以我已经在考虑在角落添加黑色方形标记..但也许有人可以给我一些如何正确做的想法。
HoughLinesP(我使用 nodejs,但我可以阅读 python 和 c++):
const imageSize = {
width: gray.cols,
height: gray.rows
};
const threshold_min = 200;
const ratio_min_max = 1;
const edges = gray.canny(threshold_min,threshold_min*ratio_min_max,3);
const minLineLength = imageSize.width / 4,
maxLineGap = 10,
threshold = 100;
const lines = edges.houghLinesP(1, Math.PI/180, threshold, minLineLength, maxLineGap);
//draw lines on the output
for( let i = 0; i < lines.length; i++ ) {
const l = lines[i];
const {x,y,z,w} = l;
output.drawLine(
cv.Point(w, x), …Run Code Online (Sandbox Code Playgroud)我有 2 个轮廓(cont1和cont2)从cv2.findContours(). 我怎么知道它们是否相交?我不需要坐标,我只需要一个布尔值True或False.
我尝试了不同的方法,并且已经尝试与
if ((cont1 & cont2).area() > 0):
Run Code Online (Sandbox Code Playgroud)
...但得到数组没有方法“Area()”的错误
...
cont1array = cv2.findContours(binary1, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
cont2array = cv2.findContours(binary2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
...
for cont1 in cont1array:
for cont2 in cont2array:
print("cont1")
print(cont1)
print(type(cont1))
print("cont2")
print(cont2)
print(type(cont2))
> if cont1 and cont2 intersect: #i dont know how check intersect
print("yes they intersect")
else:
print("no they do not intersect")
# cont1
# [[172 302]
# [261 301]
# [262 390] …Run Code Online (Sandbox Code Playgroud) 我想计算红外相机生成的图像中形状的面积。
我有一大组矩阵,它们是由红外相机生成的。在每个矩阵/图像中,我的背景主要是一个点的图像,这是红外辐射的来源。我处理它的方法是使用 Python OpenCV 来通过消除背景和计算形状中的像素数来隔离源图像。问题是在每张图像中,图像的一部分也变成了背景,所以我无法像我想要的那样获得完整的图像。
import cv2
import numpy as np
from matplotlib import pyplot as plt
PPmm = 81/55 #Pixel per mm
img = np.genfromtxt('Image 5 Z_plane = 141.0_contour_plot.csv', delimiter= ',')
img_cv = cv2.resize(img,(81,81))
np.savetxt('testing.csv', img_cv, delimiter= ',')
img = (img_cv*255).astype(np.uint8)
edges = cv2.Canny(img,150,250)
se = np.ones((7,7), dtype='uint8')
# Perform morphology
image_close = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, se)
# Your code now applied to the closed image
cnt = cv2.findContours(image_close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
mask = np.zeros(img.shape[:2], np.uint8)
cv2.drawContours(mask, cnt, -1, 255, -1) …Run Code Online (Sandbox Code Playgroud) python opencv image-processing computer-vision opencv-contour
我有一个骨架图像(如下所示)。
我想得到线条的交点。我在下面尝试了以下方法,skeleton是一个 openCV 图像,算法返回一个坐标列表:
def getSkeletonIntersection(skeleton):
image = skeleton.copy();
image = image/255;
intersections = list();
for y in range(1,len(image)-1):
for x in range(1,len(image[y])-1):
if image[y][x] == 1:
neighbourCount = 0;
neighbours = neighbourCoords(x,y);
for n in neighbours:
if (image[n[1]][n[0]] == 1):
neighbourCount += 1;
if(neighbourCount > 2):
print(neighbourCount,x,y);
intersections.append((x,y));
return intersections;
Run Code Online (Sandbox Code Playgroud)
它找到有两个以上相邻像素的白色像素的坐标。我认为这只会返回角落,但事实并非如此 - 它会返回更多点。
这是其检测到的点标记在图像上的输出。这是因为它检测到下面显示的一些不是相交的示例。
0 0 0 1 1 0 0 1 1
1 1 1 0 1 0 1 1 0
0 0 1 0 0 …Run Code Online (Sandbox Code Playgroud) python opencv image-processing line-intersection opencv-contour
我正在使用openCV和python从图像中提取轮廓.现在我需要将这些轮廓路径(列表)导出为svg路径.我怎样才能做到这一点?
码:
ret,thresh = cv2.threshold(imgray,27,25,0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_TC89_L1)
print(type(contours)) #type list
Run Code Online (Sandbox Code Playgroud)