如何将使用c#和opencvsharp完成的代码更改为javacv [英] How to change a code completed with c# and opencvsharp into javacv

查看:142
本文介绍了如何将使用c#和opencvsharp完成的代码更改为javacv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Windows.Forms;
使用 System.IO;

使用 OpenCvSharp;

namespace LaneDetection
{
class Program
{
[STAThread]
静态 void Main()
{
CvCapture cap = CvCapture.FromFile( road.avi);
CvWindow w = new CvWindow( Lane检测);
IplImage src,grey,dstCanny,halfFrame,smallImg;
CvMemStorage storage = new CvMemStorage();
CvSeq线;
CvHaarClassifierCascade cascade = CvHaarClassifierCascade.FromFile( haarcascade_cars3.xml);

const double Scale = 2 0 ;
const double ScaleFactor = 1 05 ;
const int MinNeighbors = 2 ;

CvSeq< cvavgcomp>汽车;

while (CvWindow.WaitKey( 10 < 0
{
src = cap.QueryFrame();
halfFrame = new IplImage( new CvSize(src.Size.Width / 2 ,src.Size.Height / 2 ),BitDepth.U8, 3 );
Cv.PyrDown(src,halfFrame,CvFilter.Gaussian5x5);

gray = new IplImage(src.Size,BitDepth.U8, 1 );
dstCanny = new IplImage(src.Size,BitDepth.U8, 1 );


smallImg = new IplImage( new CvSize(Cv。 Round(src.Width / Scale),Cv.Round(src.Height / Scale)),BitDepth.U8, 1 );
使用(IplImage gray = new IplImage(src.Size,BitDepth.U8, 1 ))
{
Cv.CvtColor(src,gray,ColorConversion.BgrToGray);
Cv.Resize(灰色,smallImg,Interpolation.Linear);
Cv.EqualizeHist(smallImg,smallImg);
}

storage.Clear();
cars = Cv.HaarDetectObjects(smallImg,cascade,storage,ScaleFactor,MinNeighbors,HaarDetectionType.DoCannyPruning, new CvSize( 30 30 ));

for int i = 0 ; i < cars.Total; i ++)
{
CvRect r = cars [i] .Value.Rect ;
CvPoint center = new CvPoint
{
X = Cv.Round((rX + r.Width * 0 5 )* Scale),
Y = Cv.Round((rY + r.Height * 0 5 )* Scale)
};
int radius = Cv.Round((r.Width + r.Height)* 0 25 * Scale);
src.Circle(center,radius,CvColor.Blue, 2 ,LineType.AntiAlias, 0 );
}

// 裁剪图像的上半部分,因为我们只是感兴趣的是视频的下半部分
int halfWidth = src.Width / 2 ;
int halfHeight = src.Height / 2 ;
int startX = halfWidth - (halfWidth / 2 );
src.SetROI( new CvRect( 0 ,halfHeight - 0 ,src.Width - 1 ,src.Height - 1 )) ;

gray.SetROI(src.GetROI());
dstCanny.SetROI(src.GetROI());

src.CvtColor(gray,ColorConversion.BgrToGray);
Cv.Smooth(灰色,灰色,SmoothType.Gaussian, 5 5 );
Cv.Canny(灰色,dstCanny, 50 200 ,ApertureSize.Size3);

storage.Clear();
lines = dstCanny.HoughLines2(storage,HoughLinesMethod.Probabilistic, 1 ,Math.PI / 180 50 50 100 ) ;

for int i = 0 ; i < lines.Total; i ++)
{
CvLineSegmentPoint elem = lines.GetSeqElem< cvlinesegmentpoint>(i )。值;

int dx = elem.P2.X - elem.P1.X;
int dy = elem.P2.Y - elem.P1.Y;
double angle = Math.Atan2(dy,dx)* 180 / Math.PI;

if (Math.Abs​​(angle)< = 10
继续;

if (elem.P1.Y > elem.P2.Y + 50 || elem.P1.Y < elem.P2.Y - 50
{
src.Line(elem.P1,elem.P2,CvColor.Red, 2 ,LineType.AntiAlias, 0 );
}
}

src.ResetROI();

storage.Clear();
w.Image = src;
}
}
}
}

解决方案

我建​​议,使用支持java的OpenCV 2.4.9。





[ ^ ]



这里是示例,如何使用Opencv for java

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;

using OpenCvSharp;

namespace LaneDetection
{
    class Program
    {
        [STAThread]
        static void Main()
        {
            CvCapture cap = CvCapture.FromFile("road.avi");
            CvWindow w = new CvWindow("Lane Detection");
            IplImage src, gray, dstCanny, halfFrame, smallImg;
            CvMemStorage storage = new CvMemStorage();
            CvSeq lines;
            CvHaarClassifierCascade cascade = CvHaarClassifierCascade.FromFile("haarcascade_cars3.xml");

            const double Scale = 2.0;
            const double ScaleFactor = 1.05;
            const int MinNeighbors = 2;

            CvSeq<cvavgcomp> cars;

            while (CvWindow.WaitKey(10) < 0)
            {
                src = cap.QueryFrame();
                halfFrame = new IplImage(new CvSize(src.Size.Width / 2, src.Size.Height / 2), BitDepth.U8, 3);
                Cv.PyrDown(src, halfFrame, CvFilter.Gaussian5x5);

                gray = new IplImage(src.Size, BitDepth.U8, 1);
                dstCanny = new IplImage(src.Size, BitDepth.U8, 1);


                smallImg = new IplImage(new CvSize(Cv.Round(src.Width / Scale), Cv.Round(src.Height / Scale)), BitDepth.U8, 1);
                using (IplImage grey = new IplImage(src.Size, BitDepth.U8, 1))
                {
                    Cv.CvtColor(src, grey, ColorConversion.BgrToGray);
                    Cv.Resize(grey, smallImg, Interpolation.Linear);
                    Cv.EqualizeHist(smallImg, smallImg);
                }

                storage.Clear();
                cars = Cv.HaarDetectObjects(smallImg, cascade, storage, ScaleFactor, MinNeighbors, HaarDetectionType.DoCannyPruning, new CvSize(30, 30));

                for (int i = 0; i < cars.Total; i++)
                {
                    CvRect r = cars[i].Value.Rect;
                    CvPoint center = new CvPoint
                    {
                        X = Cv.Round((r.X + r.Width * 0.5) * Scale),
                        Y = Cv.Round((r.Y + r.Height * 0.5) * Scale)
                    };
                    int radius = Cv.Round((r.Width + r.Height) * 0.25 * Scale);
                    src.Circle(center, radius, CvColor.Blue, 2, LineType.AntiAlias, 0);
                }

                // Crop off top half of image since we're only interested in the lower portion of the video
                int halfWidth = src.Width / 2;
                int halfHeight = src.Height / 2;
                int startX = halfWidth - (halfWidth / 2);
                src.SetROI(new CvRect(0, halfHeight - 0, src.Width - 1, src.Height - 1));

                gray.SetROI(src.GetROI());
                dstCanny.SetROI(src.GetROI());

                src.CvtColor(gray, ColorConversion.BgrToGray);
                Cv.Smooth(gray, gray, SmoothType.Gaussian, 5, 5);
                Cv.Canny(gray, dstCanny, 50, 200, ApertureSize.Size3);

                storage.Clear();
                lines = dstCanny.HoughLines2(storage, HoughLinesMethod.Probabilistic, 1, Math.PI / 180, 50, 50, 100);

                for (int i = 0; i < lines.Total; i++)
                {
                    CvLineSegmentPoint elem = lines.GetSeqElem<cvlinesegmentpoint>(i).Value;

                    int dx = elem.P2.X - elem.P1.X;
                    int dy = elem.P2.Y - elem.P1.Y;
                    double angle = Math.Atan2(dy, dx) * 180 / Math.PI;

                    if (Math.Abs(angle) <= 10)
                        continue;

                    if (elem.P1.Y > elem.P2.Y + 50 || elem.P1.Y < elem.P2.Y - 50)
                    {
                        src.Line(elem.P1, elem.P2, CvColor.Red, 2, LineType.AntiAlias, 0);
                    }
                }

                src.ResetROI();

                storage.Clear();
                w.Image = src;
            }
        }
    }
}

解决方案

i'll suggest that, use OpenCV 2.4.9 which support java.


[^]

here is the sample for, how to use Opencv for java.


这篇关于如何将使用c#和opencvsharp完成的代码更改为javacv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆