在上一章纠结过教程的选择之后,还是觉得老老实实从零开始学OpenCV,从OpenCV C++视频教程开始。
这里确保你已经拥有了三语言环境。而且用于CSharp的代码和C++的代码差不多,所以我会按照C++,CSharp,Python的顺序写代码
Mat是图像的基本容器,将图像信息转化为数字信息,MAT本质上是一个矩阵。
我们试试读取图片
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
//注意,文件的路径必须是反斜杠/
Mat src = imread("D:/workSpace/OpenCV/HellOpenCV/Resources/OpenCV.png");
//显示图片
imshow("C++ Png", src);
waitKey(0);
destroyAllWindows();
return 0;
}
internal class Program
{
static void Main(string[] args)
{
Mat src = Cv2.ImRead("D:/workSpace/OpenCV/HellOpenCV/Resources/OpenCV.png");
//显示图片
Cv2.ImShow("CSharp", src);
Cv2.WaitKey(0);
Cv2.DestroyAllWindows();
}
}
Python和C++最大的区别就是少了一个变量的命名
import cv2
# python中需要np来辅助
import numpy as np
src = cv2.imread("D:/workSpace/OpenCV/HellOpenCV/Resources/OpenCV.png")
cv2.imshow("Python",src)
cv2.waitKey(0)
cv2.destroyAllWindows()
OpenCV 提供了读取灰度图像的接口
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
//注意,文件的路径必须是反斜杠/
//IMREAD_GRAYSCALE灰度读取
Mat src = imread("D:/workSpace/OpenCV/HellOpenCV/Resources/OpenCV.png",IMREAD_GRAYSCALE);
//显示图片
imshow("C++", src);
waitKey(0);
destroyAllWindows();
return 0;
}
using OpenCvSharp;
namespace HelloOpenCV
{
internal class Program
{
static void Main(string[] args)
{
// 由于C# 有枚举类型,命名更加舒服
Mat src = Cv2.ImRead("D:/workSpace/OpenCV/HellOpenCV/Resources/OpenCV.png",ImreadModes.Grayscale);
//显示图片
Cv2.ImShow("CSharp", src);
Cv2.WaitKey(0);
Cv2.DestroyAllWindows();
}
}
}
import cv2
# python中需要np来辅助
import numpy as np
# IMREAD_GRAYSCALE,灰度读取
src = cv2.imread("D:/workSpace/OpenCV/HellOpenCV/Resources/OpenCV.png",cv2.IMREAD_GRAYSCALE)
cv2.imshow("Python",src)
cv2.waitKey(0)
cv2.destroyAllWindows()
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
//注意,文件的路径必须是反斜杠/
//IMREAD_GRAYSCALE灰度读取
Mat src = imread("D:/workSpace/OpenCV/HellOpenCV/Resources/OpenCV.png",IMREAD_GRAYSCALE);
//显示图片
imshow("C++", src);
cout << endl;
//打印图像信息
cout << "图像的行数为: " << src.rows << endl; //获取图像的高度,行数;
cout << "图像的列数为: " << src.cols << endl; //获取图像的宽度,列数;
cout << "图像的通道数为: " << src.channels() << endl; //获取图像的通道数,彩色图=3,灰度图=1;
cout << "图像的尺寸为: " << src.size << endl; //获取图像的尺寸,行*列;
waitKey(0);
destroyAllWindows();
return 0;
}
一般来说,只要C++跑通了,C# 的代码大差不差
using OpenCvSharp;
namespace HelloOpenCV
{
internal class Program
{
static void Main(string[] args)
{
// 由于C# 有枚举类型,命名更加舒服
Mat src = Cv2.ImRead("D:/workSpace/OpenCV/HellOpenCV/Resources/OpenCV.png",ImreadModes.Grayscale);
Console.WriteLine($"图像的行数为:{src.Rows}");
Console.WriteLine($"图像的列数为:{src.Cols}");
Console.WriteLine($"图像的通道数为:{src.Channels()}");
Console.WriteLine($"图像的尺寸为:{src.Size()}");
//显示图片
Cv2.ImShow("CSharp", src);
Cv2.WaitKey(0);
Cv2.DestroyAllWindows();
}
}
}
import cv2
# python中需要np来辅助
import numpy as np
# IMREAD_GRAYSCALE,灰度读取
src = cv2.imread("D:/workSpace/OpenCV/HellOpenCV/Resources/OpenCV.png",cv2.IMREAD_GRAYSCALE)
# 打印图片信息
# Python将其信息统一放在了shape里面,如果是单通道,通道个数直接没有
print('行,列,通道',src.shape)
print('type:',src.dtype)
# print(str.shape)
cv2.imshow("Python",src)
cv2.waitKey(0)
cv2.destroyAllWindows()
如果说C++ 版本是原版的话。那么
我感觉我的学习的路子应该是对的,毕竟是新手,三个语言的基础的算子还是都要了解一下的。