物流
opencv 2.4和Python2.7
我正在处理的图片:

问题
我感兴趣的是在9条垂直和水平线周围孤立形成盒子的轮廓。我只是不知道该怎么做。我看过不同的教程,比如数独拼图上的那些,那些简单地假设最大的盒子是你要找的那个(因为Sudoku拼图中没有方框,减去实际的网格)。我试过使用findContour函数并按大小过滤等高线,但没有结果。最后,我得到了不一致的结果,有时找到正确的轮廓,而另一些时候则发现完全错误的轮廓。谁能给我指明正确的方向?谢谢。
原始图像:

发布于 2014-12-19 09:26:41
把原来的图像放上去更好,但我试着从你的轮廓图像中解释
我做了以下步骤
你只需要平滑投影和完善你的搜索准确的轮廓。


如果您认为它有用,我可以共享opencv c++ (不是python)实现的代码。
编辑:
这是我用来做水平和垂直投影的代码,您可能需要优化它。
void HVprojection(Mat image)
{
// find the vertical projection
Mat smothedRes = image.clone();
vector<double> v_vl_proj; // holds the column sum values
double max_vl_proj_h = 0,max_vl_proj_v=0; // holds the maximum value
double average_v=0;
for( int i=0;i<image.cols;++i )
{
Mat col;
Scalar col_sum;
// get individual columns
col= image.col(i);
col_sum = sum( col ); // find the sum of ith column
v_vl_proj.push_back( col_sum.val[0] ); // push back to vector
if( col_sum.val[0]>max_vl_proj_v ) max_vl_proj_v = col_sum.val[0];
average_v+= col_sum.val[0];
}
average_v = average_v/image.cols;
// find the horizontal projection
vector<double> h_vl_proj; // holds the row sum values
double average_h=0;
for( int i=0;i<image.rows;++i )
{
Mat row;
Scalar row_sum;
// get individual columns
row= image.row(i);
row_sum = sum(row); // find the sum of ith row
h_vl_proj.push_back(row_sum.val[0]); // push back to vector
if( row_sum.val[0]>max_vl_proj_h ) max_vl_proj_h = row_sum.val[0];
average_h+= row_sum.val[0];
}
average_h = average_h/image.rows;
//******************Plotting vertical projection*******************
for(int j =1;j<image.cols;j++)
{
int y1 = int(image.rows*v_vl_proj[j-1]/max_vl_proj_v);
int y2 = int(image.rows*v_vl_proj[j]/max_vl_proj_v);
line(image,Point(j-1,y1),Point(j,y2),Scalar(255,255,255),1,8);
}
int average_y = int(image.rows*average_v/max_vl_proj_v); // zero level
line(image,Point(0,average_y),Point(image.cols,average_y),Scalar(255,255,255),1,8);
//***************Plotting horizontal projection**************
for(int j =1;j<image.rows;j++)
{
int x1 = int(0.25*image.cols*h_vl_proj[j-1]/max_vl_proj_h);
int x2 = int(0.25*image.cols*h_vl_proj[j]/max_vl_proj_h);
line(image,Point(x1,j-1),Point(x2,j),Scalar(255,0,0),1,8);
}
int average_x = int(0.25*image.cols*average_h/max_vl_proj_h);
line(image,Point(average_x,0),Point(average_x,image.rows),Scalar(255,0,0),1,8);
imshow("horizontal_projection",image);
imwrite("h_p.jpg",image);
// if you want to smooth the signal of projection in case of noisu signal
v_vl_proj = smoothing(v_vl_proj);
for(int j =1;j<image.cols;j++)
{
int y1 = int(image.rows*v_vl_proj[j-1]/max_vl_proj_v);
int y2 = int(image.rows*v_vl_proj[j]/max_vl_proj_v);
line(smothedRes,Point(j-1,y1),Point(j,y2),Scalar(0,255,0),1,8);
}
int average_y1 = int(smothedRes.rows*average_v/max_vl_proj_v); // zero level
line(smothedRes,Point(0,average_y1),Point(smothedRes.cols,average_y1),Scalar(0,255,0),1,8);
imshow("SMoothed",smothedRes);
imwrite("Vertical_projection.jpg",smothedRes);
waitKey(0);
}为了平滑投影信号:
vector<double> smoothing(vector<double> a)
{
//How many neighbors to smooth
int NO_OF_NEIGHBOURS=5;
vector<double> tmp=a;
vector<double> res=a;
for(int i=0;i<a.size();i++)
{
if(i+NO_OF_NEIGHBOURS+1<a.size())
{
for(int j=1;j<NO_OF_NEIGHBOURS;j++)
{
res.at(i)+=res.at(i+j+1);
}
res.at(i)/=NO_OF_NEIGHBOURS;
}
else
{
for(int j=1;j<NO_OF_NEIGHBOURS;j++)
{
res.at(i)+=tmp.at(i-j);
}
res.at(i)/=NO_OF_NEIGHBOURS;
}
}
return res;
}发布于 2014-12-20 08:57:05
受@dervish回答的启发,我有了一些想法。
M),使图像对齐。轴向M对最终网格进行反变换,得到原始图像空间中的网格。或者直接在步骤2中找到最后的网格位置,以得到第N条最长的线。并按步骤4过滤结果。

python代码:
import cv2
import numpy as np
def main():
im = cv2.imread('image.png')
#edge = cv2.imread('edge.png', 0)
edge = cv2.Canny(im, 100, 200, apertureSize=3)
lines = cv2.HoughLines(edge, 1, np.pi/180, 140)
for rho, theta in lines[0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(im, (x1, y1), (x2, y2), (0, 0, 255), 2)
# TODO: filter the lines by color and line distance
cv2.imshow('image', im)
cv2.imshow('edge', edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()https://stackoverflow.com/questions/27558127
复制相似问题