Compare commits
6 Commits
3ea1e48f4c
...
master
44
Arithmetic operations/bitwise.cpp
Normal file
44
Arithmetic operations/bitwise.cpp
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace cv;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
string path1 = "../img/2.jpg";
|
||||||
|
string path2 = "../img/3.jpg";
|
||||||
|
|
||||||
|
Mat img1 = imread(path1);
|
||||||
|
Mat img2 = imread(path2);
|
||||||
|
|
||||||
|
if (img1.empty() || img2.empty())
|
||||||
|
{
|
||||||
|
cout << "无法读取图片,请检查路径!" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (img1.size() != img2.size())
|
||||||
|
{
|
||||||
|
cout << "尺寸不一致,正在自动调整 img2 的尺寸..." << endl;
|
||||||
|
// 将 img2 缩放到与 img1 相同的尺寸
|
||||||
|
resize(img2, img2, img1.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat result;
|
||||||
|
// bitwise_and() 按位与操作 掩码操作、图像分割
|
||||||
|
// bitwise_or() 按位或操作 图像叠加
|
||||||
|
// bitwise_not() 按位取反操作 图像反色
|
||||||
|
// bitwise_xor() 按位异或操作 图像差异检测
|
||||||
|
bitwise_and(img1, img2, result);
|
||||||
|
|
||||||
|
imshow("Original Image 1", img1);
|
||||||
|
imshow("Original Image 2", img2);
|
||||||
|
imshow("Multiply Result", result);
|
||||||
|
|
||||||
|
// 等待按键后关闭窗口
|
||||||
|
waitKey(0);
|
||||||
|
destroyAllWindows();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
40
Arithmetic operations/multiply.cpp
Normal file
40
Arithmetic operations/multiply.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace cv;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
string path1 = "../img/2.jpg";
|
||||||
|
string path2 = "../img/3.jpg";
|
||||||
|
|
||||||
|
Mat img1 = imread(path1);
|
||||||
|
Mat img2 = imread(path2);
|
||||||
|
|
||||||
|
if (img1.empty() || img2.empty())
|
||||||
|
{
|
||||||
|
cout << "无法读取图片,请检查路径!" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (img1.size() != img2.size())
|
||||||
|
{
|
||||||
|
cout << "尺寸不一致,正在自动调整 img2 的尺寸..." << endl;
|
||||||
|
// 将 img2 缩放到与 img1 相同的尺寸
|
||||||
|
resize(img2, img2, img1.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat result;
|
||||||
|
multiply(img1, img2, result);
|
||||||
|
|
||||||
|
imshow("Original Image 1", img1);
|
||||||
|
imshow("Original Image 2", img2);
|
||||||
|
imshow("Multiply Result", result);
|
||||||
|
|
||||||
|
// 等待按键后关闭窗口
|
||||||
|
waitKey(0);
|
||||||
|
destroyAllWindows();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
41
Arithmetic operations/plus.cpp
Normal file
41
Arithmetic operations/plus.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
// 加法
|
||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace cv;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
string path1 = "../img/2.jpg";
|
||||||
|
string path2 = "../img/3.jpg";
|
||||||
|
|
||||||
|
Mat img1 = imread(path1);
|
||||||
|
Mat img2 = imread(path2);
|
||||||
|
|
||||||
|
if (img1.empty() || img2.empty())
|
||||||
|
{
|
||||||
|
cout << "无法读取图片,请检查路径!" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (img1.size() != img2.size())
|
||||||
|
{
|
||||||
|
cout << "尺寸不一致,正在自动调整 img2 的尺寸..." << endl;
|
||||||
|
// 将 img2 缩放到与 img1 相同的尺寸
|
||||||
|
resize(img2, img2, img1.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat result;
|
||||||
|
add(img1, img2, result);
|
||||||
|
|
||||||
|
imshow("Original Image 1", img1);
|
||||||
|
imshow("Original Image 2", img2);
|
||||||
|
imshow("Add Result", result);
|
||||||
|
|
||||||
|
// 等待按键后关闭窗口
|
||||||
|
waitKey(0);
|
||||||
|
destroyAllWindows();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
41
Arithmetic operations/subtract.cpp
Normal file
41
Arithmetic operations/subtract.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
// 减法,与加法基本一样,只把add改成了subtract.
|
||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace cv;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
string path1 = "../img/2.jpg";
|
||||||
|
string path2 = "../img/3.jpg";
|
||||||
|
|
||||||
|
Mat img1 = imread(path1);
|
||||||
|
Mat img2 = imread(path2);
|
||||||
|
|
||||||
|
if (img1.empty() || img2.empty())
|
||||||
|
{
|
||||||
|
cout << "无法读取图片,请检查路径!" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (img1.size() != img2.size())
|
||||||
|
{
|
||||||
|
cout << "尺寸不一致,正在自动调整 img2 的尺寸..." << endl;
|
||||||
|
// 将 img2 缩放到与 img1 相同的尺寸
|
||||||
|
resize(img2, img2, img1.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat result;
|
||||||
|
subtract(img1, img2, result);
|
||||||
|
|
||||||
|
imshow("Original Image 1", img1);
|
||||||
|
imshow("Original Image 2", img2);
|
||||||
|
imshow("Subtract Result", result);
|
||||||
|
|
||||||
|
// 等待按键后关闭窗口
|
||||||
|
waitKey(0);
|
||||||
|
destroyAllWindows();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
14
Blur/bilateral.cpp
Normal file
14
Blur/bilateral.cpp
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cv::Mat src = cv::imread("image.jpg");
|
||||||
|
cv::Mat dst;
|
||||||
|
|
||||||
|
// 参数:输入, 输出, 邻域直径, 颜色空间标准差, 坐标空间标准差
|
||||||
|
// 颜色标准差越大,表示越宽的颜色范围会被混合
|
||||||
|
cv::bilateralFilter(src, dst, 9, 75, 75);
|
||||||
|
|
||||||
|
cv::imshow("Bilateral Filter", dst);
|
||||||
|
cv::waitKey(0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
13
Blur/blur.cpp
Normal file
13
Blur/blur.cpp
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cv::Mat src = cv::imread("../img/2.jpg");
|
||||||
|
cv::Mat dst;
|
||||||
|
|
||||||
|
// 参数:输入, 输出, 卷积核大小 Size(w, h)
|
||||||
|
cv::blur(src, dst, cv::Size(5, 5));
|
||||||
|
|
||||||
|
cv::imshow("Mean Blur", dst);
|
||||||
|
cv::waitKey(0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
14
Blur/gauss.cpp
Normal file
14
Blur/gauss.cpp
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cv::Mat src = cv::imread("image.jpg");
|
||||||
|
cv::Mat dst;
|
||||||
|
|
||||||
|
// 参数:输入, 输出, 核大小, sigmaX(标准差), sigmaY
|
||||||
|
// 如果 sigmaY 为 0,则默认与 sigmaX 相等
|
||||||
|
cv::GaussianBlur(src, dst, cv::Size(5, 5), 0);
|
||||||
|
|
||||||
|
cv::imshow("Gaussian Blur", dst);
|
||||||
|
cv::waitKey(0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
13
Blur/median.cpp
Normal file
13
Blur/median.cpp
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cv::Mat src = cv::imread("image.jpg");
|
||||||
|
cv::Mat dst;
|
||||||
|
|
||||||
|
// 参数:输入, 输出, 核大小(必须是大于1的奇数整数,如3, 5, 7)
|
||||||
|
cv::medianBlur(src, dst, 5);
|
||||||
|
|
||||||
|
cv::imshow("Median Blur", dst);
|
||||||
|
cv::waitKey(0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
34
Morphology/erode.cpp
Normal file
34
Morphology/erode.cpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace cv;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// 1. 读取图像 (0 表示以灰度模式读取)
|
||||||
|
Mat src = imread("../img/1.png", IMREAD_GRAYSCALE);
|
||||||
|
|
||||||
|
if (src.empty()) {
|
||||||
|
cout << "无法加载图像,请检查文件路径!" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 定义结构元素 (Kernel)
|
||||||
|
// 使用 getStructuringElement 创建一个 5x5 的矩形结构元素
|
||||||
|
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
|
||||||
|
|
||||||
|
// 3. 执行腐蚀操作
|
||||||
|
Mat eroded_image;
|
||||||
|
// 参数:输入, 输出, 结构元素, 锚点(默认-1,-1), 迭代次数
|
||||||
|
erode(src, eroded_image, kernel, Point(-1, -1), 1);
|
||||||
|
|
||||||
|
// 4. 显示结果
|
||||||
|
imshow("Original Image", src);
|
||||||
|
imshow("Eroded Image", eroded_image);
|
||||||
|
|
||||||
|
// 等待按键后关闭窗口
|
||||||
|
waitKey(0);
|
||||||
|
destroyAllWindows();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
15
Threshold/adaptive.cpp
Normal file
15
Threshold/adaptive.cpp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cv::Mat img = cv::imread("../img/2.jpg", cv::IMREAD_GRAYSCALE);
|
||||||
|
if (img.empty()) return -1;
|
||||||
|
|
||||||
|
cv::Mat dst;
|
||||||
|
// 参数依次为:输入, 输出, 最大值, 自适应方法, 阈值类型, 邻域大小(须为奇数), 常数C
|
||||||
|
cv::adaptiveThreshold(img, dst, 255, cv::ADAPTIVE_THRESH_MEAN_C,
|
||||||
|
cv::THRESH_BINARY, 11, 2);
|
||||||
|
|
||||||
|
cv::imshow("Adaptive Threshold", dst);
|
||||||
|
cv::waitKey(0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
18
Threshold/auto.cpp
Normal file
18
Threshold/auto.cpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cv::Mat img = cv::imread("../img/2.jpg", cv::IMREAD_GRAYSCALE);
|
||||||
|
if (img.empty()) return -1;
|
||||||
|
|
||||||
|
cv::Mat dst;
|
||||||
|
// 注意:使用 THRESH_OTSU 时,输入的 thresh 参数 (0) 会被忽略
|
||||||
|
// 它是通过 “标志位组合” 来实现的:cv::THRESH_BINARY | cv::THRESH_OTSU
|
||||||
|
double otsuThresh = cv::threshold(img, dst, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
|
||||||
|
|
||||||
|
std::cout << "Otsu 自动确定的阈值是: " << otsuThresh << std::endl;
|
||||||
|
|
||||||
|
cv::imshow("Otsu Threshold", dst);
|
||||||
|
cv::waitKey(0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
20
Threshold/simple.cpp
Normal file
20
Threshold/simple.cpp
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// 读取灰度图
|
||||||
|
cv::Mat img = cv::imread("../img/2.jpg", cv::IMREAD_GRAYSCALE);
|
||||||
|
if (img.empty()) return -1;
|
||||||
|
|
||||||
|
cv::Mat dst;
|
||||||
|
double thresh = 127;
|
||||||
|
double maxVal = 255;
|
||||||
|
|
||||||
|
// 简单阈值处理:二进制阈值
|
||||||
|
// 返回值是处理时使用的阈值
|
||||||
|
double ret = cv::threshold(img, dst, thresh, maxVal, cv::THRESH_BINARY);
|
||||||
|
|
||||||
|
cv::imshow("Simple Threshold", dst);
|
||||||
|
cv::waitKey(0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -10,7 +10,8 @@ int main()
|
|||||||
string image_path = "../img/1.png";
|
string image_path = "../img/1.png";
|
||||||
Mat image = imread(image_path);
|
Mat image = imread(image_path);
|
||||||
|
|
||||||
if (image.empty()) {
|
if (image.empty())
|
||||||
|
{
|
||||||
cout << "错误:无法加载图像,请检查路径是否正确。" << endl;
|
cout << "错误:无法加载图像,请检查路径是否正确。" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -20,5 +21,4 @@ int main()
|
|||||||
cout << "B: " << (int)pixel_value[0] << " "
|
cout << "B: " << (int)pixel_value[0] << " "
|
||||||
<< "G: " << (int)pixel_value[1] << " "
|
<< "G: " << (int)pixel_value[1] << " "
|
||||||
<< "R: " << (int)pixel_value[2] << endl;
|
<< "R: " << (int)pixel_value[2] << endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,8 @@ int main()
|
|||||||
Mat image = imread(image_path);
|
Mat image = imread(image_path);
|
||||||
Mat result = image.clone();
|
Mat result = image.clone();
|
||||||
|
|
||||||
if (image.empty()) {
|
if (image.empty())
|
||||||
|
{
|
||||||
cout << "错误:无法加载图像,请检查路径是否正确。" << endl;
|
cout << "错误:无法加载图像,请检查路径是否正确。" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -24,5 +25,4 @@ int main()
|
|||||||
waitKey(0);
|
waitKey(0);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
#include <opencv2/opencv.hpp>
|
#include <opencv2/opencv.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main()
|
||||||
|
{
|
||||||
// 读取图像
|
// 读取图像
|
||||||
cv::Mat img = cv::imread("../img/1.png");
|
cv::Mat img = cv::imread("../img/1.png");
|
||||||
if (img.empty()) {
|
if (img.empty())
|
||||||
|
{
|
||||||
std::cout << "无法读取图像" << std::endl;
|
std::cout << "无法读取图像" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user