记录一下linux系统(树莓派4B)编译C++版opencv

安装:

https://blog.csdn.net/NeoZng/article/details/126065521

装完后:

查看头文件和库文件目录:
pkg-config --libs opencv4
pkg-config --cflags opencv4

查看OpenCV版本
 pkg-config --modversion opencv4

测试:

https://www.cnblogs.com/wangyarui/p/7265171.html

编译过程:

> cd test
> g++ test.cpp -o test `pkg-config --cflags --libs opencv4`
> ./test

 

测试代码:

// demo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <sstream>


#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/types_c.h>
#include <algorithm>
#include <cmath>
#include <unistd.h>
#define random(a,b) (rand()%(b-a)+a)


using namespace cv;
using namespace std;


bool compareValue_y(const Point& pt1, const Point& pt2)
{
return pt1.y < pt2.y;
}
bool compareValue_x(const Point& pt1, const Point& pt2)
{
return pt1.x < pt2.x;
}


void PointDisgus(vector<Point>& Points) {
Point t;
int n = Points.size();
int i, j;
vector<Point> OutPoints;
vector<Point> Points_ = Points;
std::sort(Points_.begin(), Points_.end(), compareValue_y);
for (vector<Point>::iterator iter = Points_.begin(); iter != Points_.end(); iter++)
{
vector<Point> PNews;
int index = iter - Points_.begin();
Point P = *iter;
for (int i = index; i < n; i++)
{
if (abs(P.y - Points_[i].y) < 12)
{
PNews.push_back(Points_[i]);
iter++;
}
}
std::sort(PNews.begin(), PNews.end(), compareValue_x);
for (auto pp : PNews) {
OutPoints.push_back(pp);
}
iter = iter - 1;
PNews.clear();
}
Points = OutPoints;
}


int wihtarea(Mat src) {
int area = 0;
int w = src.cols;
int h = src.rows;
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
if (src.at<uchar>(row, col) == 255) {
area++;
}
}
}
return area;
}


void DetectPoint(Mat Imgg, vector<Point>& Points, Mat& outimg) {
Mat img = Imgg.clone();
Mat gray, thresh, threshout;
if (img.channels() == 3)
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
cv::threshold(gray, thresh, 80, 255, THRESH_BINARY);
bitwise_not(thresh, thresh);
Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
cv::dilate(thresh, threshout, element);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
vector<cv::Rect>XYWH;
vector<int> areas;
int dis = 5;
cv::findContours(threshout, contours, hierarchy, RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));


for (int i = 0; i < contours.size(); i++)
{
Rect box, box1;
int area = cv::contourArea(contours[i]);
if (area < 25 || area > 500) {
continue;
}
box = cv::boundingRect(contours[i]);
if (box.width * box.height > 600) {
continue;
}
if ((box.width * box.height) / (area) > 1.5) {
continue;
}
if (box.width / box.height > 4 || box.width / box.height < 0.5) {
continue;
}


XYWH.push_back(box);
box1.x = box.x - dis;
box1.y = box.y - dis;
if (box1.x < 0 || box1.y < 0) {
continue;
}
box1.width = box.width + dis * 2;
box1.height = box.height + dis * 2;
if (box1.x + box1.width > gray.cols || box1.y + box1.height > gray.rows) {
continue;
}
Mat Img1 = thresh(box);
Mat Img2 = thresh(box1);
if (wihtarea(Img1) != wihtarea(Img2)) {
continue;
}
Point p;
p.x = int(box.x + box.width / 2);
p.y = int(box.y + box.height);
Points.push_back(p);
areas.push_back(area);
cv::rectangle(img, box, Scalar(255, 0, 0), 1);
outimg = img;
}
}


const char* keys =
{
"{help h usage ? | | print this message}"
"{@video | | Video file, if not defined try to use webcamera}"
};


int main(int argc, const char** argv)
{
CommandLineParser parser(argc, argv, keys);
parser.about("Reading a video and camera v1.0.0");


if (parser.has("help"))
{
parser.printMessage();
return 0;
}
String videoFile = parser.get<String>(0);


if (!parser.check())
{
parser.printErrors();
return 0;
}


VideoCapture cap;
if (videoFile != "")
{
cap.open(videoFile);// read a video file
}
else {
cap.open(0);// read the default caera
}
if (!cap.isOpened())// check if we succeeded
{
return -1;
}


namedWindow("Video", 1);
while (1)
{
Mat frame;
cap >> frame; // get a new frame from camera

//int w = frame.cols;
//int h = frame.rows;
//cv::resize(frame, frame, cv::Size(h/2, w/2));

imshow("Video", frame);
if (waitKey(30) >= 0) break;


vector<Point> Points;
Mat outimg;
DetectPoint(frame, Points, outimg);
PointDisgus(Points);
for (int k = 0; k < Points.size(); k++) {
std::string text = to_string(k);
cv::Point origin;
origin.x = Points[k].x - 8;
origin.y = Points[k].y - 13;
cv::putText(outimg, text, origin, cv::FONT_HERSHEY_COMPLEX, 0.3, cv::Scalar(0, 0, 255), 0.1);
}
cv::imwrite("./img_box.png", outimg);
}
// Release the camera or video file
destroyWindow("Video");
cap.release();
return 0;
}

 

 

posted @ 2023-03-09 20:31  cn_gzb  阅读(151)  评论(0编辑  收藏  举报