下载地址:
https://opencv.org/releases/
归一化,转vector:
Opencv C++ 归一化_jacke121的专栏-CSDN博客_c++ opencv 归一化
opencv c++ 贴图补齐实战
opencv c++ 贴图_jacke121的专栏-CSDN博客
c++ transpose:
nanodet onnx踩坑记录_jacke121的专栏-CSDN博客
c++读取视频:
python opencv 读取图片和视频_AI视觉网奇的博客-CSDN博客
目录
c++ 图像旋转 缩放
通道转换
创建图片,rect贴图,读取rect
读图,裁剪(其实是引用),画矩形框,
矩形框:
读取字符串,resize,减法,除法
最大值,最小值
乘法
除法
转flost32类型
打印类型
打印输出
float*内存拷贝
遍历图片
mat引用传参,函数内修改 初始化Mat
opencv c++ 单通道图像:
opencv 旋转_AI视觉网奇的博客-CSDN博客_opencv 旋转
缩放示例
#include
int main()
{
cv::Mat src = cv::imread("./tu/马.jpg", cv::IMREAD_UNCHANGED);
float scaleW = 0.8; // 定义新图像的大小,宽度缩小到80%
float scaleH = 0.8; //定义新图像的大小,高度缩小到80%
int width = int(src.cols * scaleW);
int height = int(src.rows * scaleH);
cv::Mat dst;
resize(src, dst, cv::Size(width, height));//缩放图像
cv::imshow("src", src);
cv::imshow("dst", dst);
cv::waitKey();
}
c++ opencv 通道转换_jacke121的专栏-CSDN博客_c++通道
int width = 360;
int height = 30;
int width_crop = 180;
cv::Mat img(height, width, CV_8UC3, cv::Scalar(0, 0, 255));
cv::Mat img2(height, width_crop, CV_8UC3, cv::Scalar(0,255, 0));
cv::Rect rect(0, 0, width_crop, height);
cv::Mat imageROI = img(rect);
img2.copyTo(imageROI, img2);
cv::imshow("result", img);
cv::waitKey(0);
Mat img = imread(R"(D:\data\tiaosheng\imgs\0001.jpg)");
int iw = img.cols;
int ih = img.rows;
int width = 360;
int height = 30;
int width_crop = 180;
cv::Mat img1(height, width, CV_8UC3, cv::Scalar(0, 0, 255));
cv::Mat img2(height, width_crop, CV_8UC3, cv::Scalar(0, 255, 0));
cv::Rect rect(0, 0, width_crop, height);
cv::Mat imageROI = img(rect);
cv::rectangle(imageROI, Rect(10, 10, 20, 30), Scalar(0, 255, 0), 1, 1, 0);
//img2.copyTo(imageROI, img2);
cv::imshow("result", img);
cv::waitKey(0);
读取rect
float aaa = r.rect.x;
float bbb = r.rect.y;
float ccc = r.rect.width;
float ddd = r.rect.height;
std::cout << "aaa " << aaa << " bbb:" << bbb<< " ccc:" << ccc << " ddd:" << ddd << std::endl;
int x = 50;
int y = 50;
int width = 50;
int height = 50;
cv::Rect rect(x, y, width, height);
cv::rectangle(matImage,Rect(100,300,20,200),Scalar(0,0,255),1,1,0);
cv::Mat row_img = cv::imread(R"(G:\rgb/000000.jpg)");
int iw = row_img.cols;
int ih = row_img.rows;
cv::Mat image;
cv::resize(row_img, image,cv::Size(INPUT_SIZE,INPUT_SIZE));
cv::Mat dst = cv::Mat::zeros(row_img.size(), CV_32FC3);
row_img.convertTo(dst, CV_32FC3);// , 10, -1);
dst = dst - 128.0;
dst = dst / 128.0;
std::cout << dst.type() << std::endl;
for (int a = 0; a < 20; a++) {
//cout << (dst.data)[a] << " ";
cout << ((float*)(dst.data))[a] << " ";
}
opencv4可以多通道计算,opencv3.4只能计算单通道图像。
Mat image1, image3;
image1 = imread("D:\\zhengjian.jpg");
if (!image1.data)
{
return 0;
}
double minv = 0.0, maxv = 0.0;
minMaxIdx(image1, &minv, &maxv);
cout << "Mat minv = " << minv << endl;
cout << "Mat maxv = " << maxv << endl;
//归一化
image_new = (image_new - minVal) / (maxVal - minVal)*255;
//转化类型
Mat dst = Mat::zeros(image_new.size(), CV_8U);
image_new.convertTo(dst, CV_8U);// , NULL, 0);
//image_new.convertTo(image_new, CV_8U, 255.0 / (maxVal - minVal), 0);
//建立空的黑图
Mat atom_image = Mat::zeros(w, w, CV_8UC3);
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
Mat image1, image2, image3;
image1 = imread("C:\\Users\\Dell\\Desktop\\5.jpg");
if (!image1.data)
{
return 0;
}
image2 = imread("C:\\Users\\Dell\\Desktop\\4.jpg");
if (!image2.data)
{
return 0;
}
resize(image2, image3, image1.size());
namedWindow("Image 1");
imshow("Image 1", image1);
namedWindow("Image 3");
imshow("Image 3", image3);
Mat result;
image3 = image3 * 0.3;
image1 = image1 * 0.9;
add(image1, image3, result);
namedWindow("result");
imshow("result", result);
waitKey(0);
return 0;
}
Mat result1;
imshow("result_o", image1);
image1 = image1 / 2;
imshow("result", image1);
cv::Mat image0 = cv::imread("../configs/bus.jpg", cv::IMREAD_UNCHANGED);
cv::Mat dst = cv::Mat::zeros(image0.size(), CV_32FC3);
image0.convertTo(dst, CV_32FC3);// , 10, -1);
以下内容转自:
Package | OpenCV C++ cv::Mat 数据类型type对应的输出值_iLOVEJohnny的博客-CSDN博客_opencv 输出mat数据类型
std::cout << img.type() << std::endl;
id对应数据类型:
+--------+----+----+----+----+------+------+------+------+
| | C1 | C2 | C3 | C4 | C(5) | C(6) | C(7) | C(8) |
+--------+----+----+----+----+------+------+------+------+
| CV_8U | 0 | 8 | 16 | 24 | 32 | 40 | 48 | 56 |
| CV_8S | 1 | 9 | 17 | 25 | 33 | 41 | 49 | 57 |
| CV_16U | 2 | 10 | 18 | 26 | 34 | 42 | 50 | 58 |
| CV_16S | 3 | 11 | 19 | 27 | 35 | 43 | 51 | 59 |
| CV_32S | 4 | 12 | 20 | 28 | 36 | 44 | 52 | 60 |
| CV_32F | 5 | 13 | 21 | 29 | 37 | 45 | 53 | 61 |
| CV_64F | 6 | 14 | 22 | 30 | 38 | 46 | 54 | 62 |
+--------+----+----+----+----+------+------+------+------+
注意,每行结尾有 error 标注。
Mat srcResize = imread(imgpath);
for (int a = 0; a < 20; a++) {
cout << (int)srcimg.at(0, a) << " ";// ok int
cout << ((float*)(srcimg.data))[a] << " ";//error
}
cout << "int \n";
cv::Mat dst = cv::Mat::zeros(srcResize.size(), CV_32FC3);
srcResize.convertTo(dst, CV_32FC3);// , 10, -1);
for (int a = 0; a < 20; a++) {
cout << (float)dst.at(0, a) << " ";// ok float
cout << ((float*)(dst.data))[a] << " ";// ok float
//cout << setprecision(3) << ((float*)(srcimg.data))[a] << " ";
}
cout << "\n CV_32FC3 \n";
for (int a = 0; a < 20; a++) {
cout << setprecision(3) << ((double*)(dst.data))[a] << " ";//error
}
这个输出错误:
cout << (dst.data)[a] << " ";
转自:memcpy - how to copy float* to float* variable - 鹅要长大 - 博客园
float* seg_segmap = new float[OUTPUT_H * OUTPUT_W];
float* temp = new float[OUTPUT_H * OUTPUT_W];
//memcpy(temp, seg_segmap, sizeof(seg_segmap));//error
memcpy(temp, seg_segmap, OUTPUT_H * OUTPUT_W*sizeof(float));
cv::String path = R"(E:\data\Circle-detection-master\train_repeat\image/)";
cv::String dst = "result/";
//cv::String GT = "D:/astudy/dataset/circle/temp/GT/";
T test_threshold;
vector Filenames;
cv::glob(path, Filenames);
float fmeasureSum = 0.0;
float precisionSum = 0.0;
float recallSum = 0.0;
float timeSum = 0.0;
// Detect each image in the directory 'Images1'
for (int i = 0; i < Filenames.size(); i++)
{
//read images
cv::String file = Filenames[i];
void to_mat(Mat& out) {
//out = Mat::eye(300, 400, CV_8UC1);
out = Mat(300, 600, CV_8UC3);
randu(out, Scalar::all(0), Scalar::all(255));
}
int main()
{
Mat in_mat;
to_mat(in_mat);
cv::imshow("image_" + std::to_string(0), in_mat);
cv::waitKey(0);
}
初始化Mat
1. 首先是 eye, ones, zeros
Mat eye = Mat::eye(4,4,CV_8U);
Mat ones = Mat::ones(4,4,CV_8U);
Mat zeros = Mat::zeros(4,4,CV_8U);
2. 直接初始化Mat的元素,小矩阵很方便
Mat img = (Mat_(3,3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
3. create分配内存,不初始化元素
Mat m;
m.create(4, 4, CV_8U);
cout << m << endl;
4. 随机数矩阵 randn
Mat r = Mat(3,3,CV_8UC3);
randu(r, Scalar::all(0), Scalar::all(255));
cout << r << endl;
原文链接:https://blog.csdn.net/billbliss/article/details/44699641
赋值与遍历:
cv::Mat mat_box(128, 128, CV_32FC1);
for (int ii = 0; ii < mat_box.rows; ++ii)
{
for (int j = 0; j < mat_box.cols; ++j)
{
if (ii % 2 == 0 ) {
if (j % 2 == 0) {
mat_box.at(ii, j) = 0.1f;
}
else {
mat_box.at(ii, j) = 0.2f;
}
}
else {
if (j % 2 == 0) {
mat_box.at(ii, j) = 0.3f;
}
else {
mat_box.at(ii, j) = 0.4f;
}
}
}
}
unsigned char* pimage = image.data;
float* box_img = (float*)mat_box.data;
float* phost_b = input_data_host + image_area * 0 +i * input_channel * input_height * input_width;
float* phost_g = input_data_host + image_area * 1 + i * input_channel * input_height * input_width;
float* phost_r = input_data_host + image_area * 2 + i * input_channel * input_height * input_width;
float* phost_box = input_data_host + image_area * 3 + i * input_channel * input_height * input_width;
for (int i = 0; i < image_area; ++i, pimage +=3) {
*phost_r++ = (pimage[0] / 255.0f - mean[0]) / std[0];
*phost_g++ = (pimage[1] / 255.0f - mean[1]) / std[1];
*phost_b++ = (pimage[2] / 255.0f - mean[2]) / std[2];
box_img += 1;
*phost_box++ = box_img[0];
}
}