java获取轮廓的长宽_如何在javacv中提取轮廓的宽度和高度?

我正在使用

javacv包(Opencv)开发组件识别项目.我使用一种方法将图像上的矩形集返回为“CvSeq”我需要知道的是如何做以下事情

>如何从方法输出(来自CvSeq)获取每个矩形?

>如何访问矩形的长度和宽度?

这是返回矩形的方法

public static CvSeq findSquares( final IplImage src, CvMemStorage storage)

{

CvSeq squares = new CvContour();

squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);

IplImage pyr = null, timg = null, gray = null, tgray;

timg = cvCloneImage(src);

CvSize sz = cvSize(src.width() & -2, src.height() & -2);

tgray = cvCreateImage(sz, src.depth(), 1);

gray = cvCreateImage(sz, src.depth(), 1);

pyr = cvCreateImage(cvSize(sz.width()/2, sz.height()/2), src.depth(), src.nChannels());

// down-scale and upscale the image to filter out the noise

cvPyrDown(timg, pyr, CV_GAUSSIAN_5x5);

cvPyrUp(pyr, timg, CV_GAUSSIAN_5x5);

cvSaveImage("ha.jpg", timg);

CvSeq contours = new CvContour();

// request closing of the application when the image window is closed

// show image on window

// find squares in every color plane of the image

for( int c = 0; c < 3; c++ )

{

IplImage channels[] = {cvCreateImage(sz, 8, 1), cvCreateImage(sz, 8, 1), cvCreateImage(sz, 8, 1)};

channels[c] = cvCreateImage(sz, 8, 1);

if(src.nChannels() > 1){

cvSplit(timg, channels[0], channels[1], channels[2], null);

}else{

tgray = cvCloneImage(timg);

}

tgray = channels[c]; // try several threshold levels

for( int l = 0; l < N; l++ )

{

// hack: use Canny instead of zero threshold level.

// Canny helps to catch squares with gradient shading

if( l == 0 )

{

// apply Canny. Take the upper threshold from slider

// and set the lower to 0 (which forces edges merging)

cvCanny(tgray, gray, 0, thresh, 5);

// dilate canny output to remove potential

// // holes between edge segments

cvDilate(gray, gray, null, 1);

}

else

{

// apply threshold if l!=0:

cvThreshold(tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY);

}

// find contours and store them all as a list

cvFindContours(gray, storage, contours, sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

CvSeq approx;

// test each contour

while (contours != null && !contours.isNull()) {

if (contours.elem_size() > 0) {

approx = cvApproxPoly(contours, Loader.sizeof(CvContour.class),storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0);

if( approx.total() == 4

&&

Math.abs(cvContourArea(approx, CV_WHOLE_SEQ, 0)) > 1000 &&

cvCheckContourConvexity(approx) != 0

){

double maxCosine = 0;

//

for( int j = 2; j < 5; j++ )

{

// find the maximum cosine of the angle between joint edges

double cosine = Math.abs(angle(new CvPoint(cvGetSeqElem(approx, j%4)), new CvPoint(cvGetSeqElem(approx, j-2)), new CvPoint(cvGetSeqElem(approx, j-1))));

maxCosine = Math.max(maxCosine, cosine);

}

if( maxCosine < 0.2 ){

cvSeqPush(squares, approx);

}

}

}

contours = contours.h_next();

}

contours = new CvContour();

}

}

return squares;

}

这是我使用的示例原始图像

这是我在匹配的矩形周围绘制线条后得到的图像

实际上在上面的图像我想要删除那些大矩形,只需要识别其他矩形,所以我需要一些代码示例来了解如何归档上述目标.请善意与我分享您的经验.谢谢 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Java,可以使用JavaCV库进行边缘检测和轮廓提取JavaCV是一个基于OpenCV和FFmpeg的Java接口库,可以用于图像和视频处理。以下是一个简单的JavaCV边缘检测和轮廓提取的示例代码: ```java import org.bytedeco.javacpp.opencv_core.*; import org.bytedeco.javacpp.opencv_imgcodecs; import org.bytedeco.javacpp.opencv_imgproc; public class EdgeDetectionExample { public static void main(String[] args) { // Load image Mat image = opencv_imgcodecs.imread("input.jpg"); // Convert to grayscale Mat grayImage = new Mat(); opencv_imgproc.cvtColor(image, grayImage, opencv_imgproc.CV_BGR2GRAY); // Apply Canny edge detection Mat edges = new Mat(); opencv_imgproc.Canny(grayImage, edges, 100, 200); // Find contours MatVector contours = new MatVector(); Mat hierarchy = new Mat(); opencv_imgproc.findContours(edges, contours, hierarchy, opencv_imgproc.CV_RETR_EXTERNAL, opencv_imgproc.CV_CHAIN_APPROX_SIMPLE); // Draw contours on original image Mat result = new Mat(); opencv_core.Scalar color = new opencv_core.Scalar(0, 255, 0, 0); opencv_imgproc.drawContours(image, contours, -1, color, 2, opencv_imgproc.LINE_8, hierarchy, 0, new opencv_core.Point()); // Save result opencv_imgcodecs.imwrite("output.jpg", image); } } ``` 对于Matlab,可以使用以下代码进行图像边缘提取: ```matlab % Load image image = imread('input.jpg'); % Convert to grayscale grayImage = rgb2gray(image); % Apply edge detection using Canny method edges = edge(grayImage, 'canny'); % Find contours [B,L] = bwboundaries(edges, 'noholes'); % Draw contours on original image imshow(image); hold on; for i=1:length(B) boundary = B{i}; plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2); end ``` 这里使用了Canny边缘检测方法和bwboundaries函数来提取轮廓。在显示轮廓时,使用了plot函数绘制轮廓边界。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值