欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

opencv

程序员文章站 2024-03-25 10:18:28
...

#include
#include
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include
#include

#include <stdlib.h>
using namespace std;
using namespace cv;

int bSums(Mat src)
{

int counter = 0;
//迭代器访问像素点
Mat_<uchar>::iterator it = src.begin<uchar>();
Mat_<uchar>::iterator itend = src.end<uchar>();
for (; it!=itend; ++it)
{
    if((*it)>0) counter+=1;//二值化后,像素点是0或者255
}
return counter;

}

vector Flip(Mat &img)
{
int rows=img.rows;
int cols=img.cols;

vector<int> vec;
vector<int> abs;
vector<int> vec_i;
int k=0;
for(int i=10; i<cols; i=i+5)
{


        Mat temp(img(Rect(i,0,10,rows)));
        Mat result;
        threshold(temp, result, 80, 255, CV_THRESH_BINARY);

        int a = bSums(result);    //guangcha


        vec.push_back(a);
        int b=vec[k-1];

        int c;
        c=a-b;
        if(c<0)
        {
            c=-c;
        }

        if(c>5)
        {
         abs.push_back(i);
        }

        if(i+15>cols)
        {
            break;

        }
        k=k+1;
}
return abs;

}

void roi(Mat src,int x1,int y1,int x2,int y2,int d)
{
// Mat temp(src(Rect(x1,y1-d,x2,y2+d)));;
// rectangle(src, (x1,y1),(x2,y2), Scalar(0, 255, 0), 2);
Rect roi = Rect(Point(x1, y1), Point(x2, y2));
Mat roiImg = src(roi);
imshow(“new”,roiImg);
waitKey(0);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// static int num=0;
// static int right;
// static int left;
// vector sequence;
// vector eq;
// vector point1;
// vector point2;

Mat src1 = imread( "/home/zyx/Downloads/626900593.jpg" );

// Mat result;
// threshold(src1, result, 80, 255.0, THRESH_TOZERO);

// sequence=Flip(src1);
// sequence.erase(sequence.begin());

// //访问该vector的值的时候,也是直接访问即可
// for(vector::iterator it=sequence.begin();it!=sequence.end();it++)
// {

// int a=*(it-1);
// int b=*it;
// if(b-a>10)
// {
// eq.push_back(b);

// }
// }

// for(int i=0;i<eq.size();i++) // push_back(elem)在数组最后添加数据
// {
// int static right,left;
// //cout<<eq[i]<<",";
// if(i % 2 == 0){
// right=eq[i];
// }else{
// left=eq[i];
// cout<<right<<" ,"<<left<<endl;
// rectangle(result,cvPoint(right, 0),cvPoint(left, 50),Scalar(255,255,0),1,1,0);
// }

// }
// imshow(“二值化图像”, result);
// waitKey(0);
roi(src1,0,0,800,430,10);
cout<<src1.rows<<endl;
cout<<src1.cols<<endl;
return 0;
return a.exec();
}

#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include
#include <stdlib.h>
IplImage* src=0;
void on_mouse( int event, int x, int y, int flags, void* ustc)
{
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);//字体结构初始化

if((event == CV_EVENT_LBUTTONDOWN)&&(flags))//鼠标左键按下事件发生
{
    CvPoint pt = cvPoint(x,y);//获取当前点的横纵坐标值
    char temp[16];
    sprintf(temp,"(%d,%d)",pt.x,pt.y);//打印当前坐标值

    printf("( %d, %d) ",pt.x,pt.y);

    cvPutText(src,temp, pt, &font, cvScalar(255, 255, 255, 0)); //在图像中打印当前坐标值
    cvCircle( src, pt, 2,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );//在在图像当前坐标点下画圆
    cvShowImage( "src", src );
}

}

int main()
{
src = cvLoadImage("/home/zyx/Downloads/626900593.jpg",1); //读入图像

cvNamedWindow("src",1);//新建窗口
cvSetMouseCallback( "src", on_mouse, 0 );  //注册鼠标相应回调函数
cvShowImage("src",src);
cvWaitKey(0);
cvDestroyAllWindows();//销毁所有窗口
cvReleaseImage(&src);//释放图像数据
return 0;

}