#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int threshold_v = 226;
int threshold_max = 255;
Mat src, gray_src, drawImg;
const char* output_win = "rectangle-demo";
void Contours_Callback(int, void*);
Scalar color = Scalar(0, 0, 255);
int main(int argc, char** argv) {
src = imread(".jpg");
namedWindow(output_win, CV_WINDOW_AUTOSIZE);
Mat imgRGB = src.clone();
for (int i = 0; i < imgRGB.rows; i++)
{
for (int j = 0; j < imgRGB.cols; j++)
{
int B = imgRGB.at<Vec3b>(i, j)[0];
int G = imgRGB.at<Vec3b>(i, j)[1];
int R = imgRGB.at<Vec3b>(i, j)[2];
if ((B > 30) && (B < 50) && (G >= 20) && (G <= 40) && (R >= 120) && (R <= 160) || (B >= 230) && (B < 255) && (G >= 230) && (G <= 255) && (R >= 230) && (R <= 255))
{
imgRGB.at<Vec3b>(i, j)[0] = 255;
imgRGB.at<Vec3b>(i, j)[1] = 255;
imgRGB.at<Vec3b>(i, j)[2] = 255;
}
else {
imgRGB.at<Vec3b>(i, j)[0] = 0;
imgRGB.at<Vec3b>(i, j)[1] = 0;
imgRGB.at<Vec3b>(i, j)[2] = 0;
}
}
}
cvtColor(imgRGB, gray_src, CV_BGR2GRAY);
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
morphologyEx(gray_src, gray_src, CV_MOP_OPEN, kernel);
createTrackbar("Threshold Value:", output_win, &threshold_v, threshold_max, Contours_Callback);
Contours_Callback(0, 0);
waitKey(0);
return 0;
}
void Contours_Callback(int, void*) {
Mat binary_output;
vector<vector<Point>>contours;
vector<Vec4i>hierachy;
threshold(gray_src, binary_output, threshold_v, threshold_max, THRESH_BINARY);
findContours(binary_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(-1, -1));
vector<vector<Point>>contours_ploy(contours.size());
vector<RotatedRect> minRects(contours.size());
for (size_t i = 0; i < contours.size(); i++) {
approxPolyDP(Mat(contours[i]), contours_ploy[i], 3, true);
if (contours_ploy[i].size() > 5) {
minRects[i] = minAreaRect(contours_ploy[i]);
}
}
Point2f pts1[4];
Point2f pts2[4];
src.copyTo(drawImg);
for (size_t t = 0; t < contours.size(); t++) {
if (contours_ploy[t].size() > 5) {
if (t == 0) {
minRects[t].points(pts1);
}
else if (t == 1) {
minRects[t].points(pts2);
}
}
}
if (pts2[2].x != pts2[3].x&& pts1[0].x != pts1[1].x) {
line(drawImg, pts1[0], pts1[1], color, 2, 8);
line(drawImg, pts1[1], pts2[2], color, 2, 8);
line(drawImg, pts2[2], pts2[3], color, 2, 8);
line(drawImg, pts2[3], pts1[0], color, 2, 8);
}
imshow(output_win, drawImg);
}
double theta = -atan(static_cast<double>(ptz_camera_y + barrel_ptz_offset_y))/static_cast<double>(overlap_dist);
double r_data[] = {1,0,0,0,cos(theta),sin(theta),0,-sin(theta),cos(theta)};
double t_data[] = {static_cast<double>(ptz_camera_x),static_cast<double>(ptz_camera_y),static_cast<double>(ptz_camera_z)};
Mat t_camera_ptz(3,1,CV_64FC1,t_data);
Mat r_camera_ptz(3,3,CV_64FC1,r_data);
Mat position_in_ptz;
position_in_ptz = r_camera_ptz * tvec - t_camera_ptz;
识别装甲板代码(早期)
于 2023-03-12 17:17:07 首次发布