#pragma execution_character_set("utf-8")
#include <QCoreApplication>
#include <QImage>
#include <QDebug>
#include <iostream>
#include <QDateTime>
#include <opencv2/opencv.hpp>
void productGrayImage()
{
cv::Mat image(10, 10, CV_8UC3);
// 遍历图像的每个像素
for (int x = 0; x < image.rows; ++x) {
for (int y = 0; y < image.cols; ++y) {
// 获取像素的指针
cv::Vec3b& pixel = image.at<cv::Vec3b>(x, y);
// 为BGR通道分别赋值
pixel[0] = 255; // 蓝色通道 (B)
pixel[1] = 9; // 绿色通道 (G)
pixel[2] = 10; // 红色通道 (R)
}
}
image.at<cv::Vec3b>(0, 0)[0] = 255;
image.at<cv::Vec3b>(0, 0)[1] = 255;
image.at<cv::Vec3b>(0, 0)[2] = 255;
printf("image.type=%d\n", image.type());
// 显示图像
cv::imshow("Colored Image", image);
cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
printf("image.type=%d\n", image.type());
for (int x = 0; x < image.rows; ++x)
{
for (int y = 0; y < image.cols; ++y)
{
// 获取像素的指针
int pixel = image.at<uchar>(x, y);
printf("%d ",pixel);
}
printf("\n");
}
// cv::imwrite("gray.jpg", image);
cv::imshow("gray Image", image);
cv::imwrite("gray.bmp", image);
}
void productBinaryImage()
{
cv::Mat image(10, 10, CV_8UC3);
// 遍历图像的每个像素
for (int x = 0; x < image.rows; ++x) {
for (int y = 0; y < image.cols; ++y) {
// 获取像素的指针
cv::Vec3b& pixel = image.at<cv::Vec3b>(x, y);
// 为BGR通道分别赋值
pixel[0] = 255; // 蓝色通道 (B)
pixel[1] = 9; // 绿色通道 (G)
pixel[2] = 10; // 红色通道 (R)
}
}
image.at<cv::Vec3b>(0, 0)[0] = 255;
image.at<cv::Vec3b>(0, 0)[1] = 255;
image.at<cv::Vec3b>(0, 0)[2] = 255;
printf("image.type=%d\n", image.type());
// 显示图像
cv::imshow("Colored Image", image);
cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
cv::threshold(image, image, 90, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
printf("image.type=%d\n", image.type());
for (int x = 0; x < image.rows; ++x)
{
for (int y = 0; y < image.cols; ++y)
{
// 获取像素的指针
int pixel = image.at<uchar>(x, y);
//printf("x=%d,y=%d,pixel=%d ", x, y, pixel);
printf("%d ",pixel);
}
printf("\n");
}
cv::imshow("binary Image", image);
cv::imwrite("binary.bmp", image);
}
//显示最简单的图片:
void showSimpleImage()
{
//CV_8UC3就是8位无符号整数,3通道,RGB的颜色模式
cv::Mat image(10, 10, CV_8UC3);
//遍历图像的每个像素
for (int x = 0; x < image.rows; ++x)
{
for (int y = 0; y < image.cols; ++y)
{
// 获取像素的指针
cv::Vec3b& pixel = image.at<cv::Vec3b>(x, y);
// 为BGR通道分别赋值
pixel[0] = 255; // 蓝色通道 (B)
pixel[1] = 0; // 绿色通道 (G)
pixel[2] = 0; // 红色通道 (R)
}
}
//最左上角的点为 红色
image.at<cv::Vec3b>(0, 0)[0] = 0;
image.at<cv::Vec3b>(0, 0)[1] = 0;
image.at<cv::Vec3b>(0, 0)[2] = 255;
printf("image.type=%d\n", image.type());
// 显示图像
cv::imshow("imageOrigin", image);
cv::imwrite("imageOrigin.bmp", image);
}
bool isBinaryImage(const cv::Mat &image) {
qDebug("enter function isBinaryImage date=%s", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz").toStdString().c_str());
if (image.channels() != 1) {
return false; // 非灰度图像不可能是二值化图像
}
// 遍历图像中的每个像素,检查值是否只有0和255
for (int i = 0; i < image.rows; ++i) {
for (int j = 0; j < image.cols; ++j) {
if (image.at<uchar>(i, j) != 0 && image.at<uchar>(i, j) != 255) {
return false; // 发现非二值化像素
}
}
}
qDebug("exit function isBinaryImage date=%s", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz").toStdString().c_str());
return true; // 所有像素均为0或255,是二值化图像
}
bool isGrayImage(const cv::Mat& image) {
qDebug("enter function isGrayImage date=%s", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz").toStdString().c_str());
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
cv::Vec3b pixel = image.at<cv::Vec3b>(i, j);
if (pixel[0] != pixel[1] || pixel[1] != pixel[2]) {
return false;
}
}
}
qDebug("exit function isGrayImage date=%s", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz").toStdString().c_str());
return true;
}
void judgeGrayImageInfo(QString imagePath)
{
//cv::Mat image = cv::imread(imagePath.toStdString(), cv::IMREAD_GRAYSCALE); // 加载图像
QImage image = QImage(imagePath);
qDebug()<<"image.colorCount="<<image.colorCount();
qDebug()<<"image.format="<<image.format();
cv::Mat mat = cv::imread(imagePath.toStdString()); // 加载图像
qDebug()<<"mat.type="<<mat.type();
for (int i = 0; i < mat.rows; i++)
{
for (int j = 0; j < mat.cols; j++)
{
if(mat.type() == 16)
{
cv::Vec3b pixel = mat.at<cv::Vec3b>(i, j);
printf("%d,%d,%d ", pixel[0], pixel[1], pixel[2]);
}
else
{
int pixel = mat.at<uchar>(i, j);
printf("%d ", pixel);
}
}
printf("\n");
}
if (isGrayImage(mat)) {
std::cout << "The image is grayscale." << std::endl;
} else {
std::cout << "The image is not grayscale." << std::endl;
}
cv::imshow("gray Image", mat);
cv::Mat mats[3];
split(mat,mats);
cv::imshow("gray gray Image", mat);
mat = mats[0];
int uniqueColors = cv::countNonZero(mat);
qDebug()<<"uniqueColors="<<uniqueColors;
qDebug()<<"mat.type="<<mat.type();
if(mat.type() == 0)
{
mat.at<uchar>(0, 1) = 255;
mat.at<uchar>(0, 2) = 255;
}
for (int i = 0; i < mat.rows; i++)
{
for (int j = 0; j < mat.cols; j++)
{
if(mat.type() == 16)
{
cv::Vec3b pixel = mat.at<cv::Vec3b>(i, j);
printf("%d,%d,%d ", pixel[0], pixel[1], pixel[2]);
}
else
{
int pixel = mat.at<uchar>(i, j);
printf("%d ", pixel);
}
}
printf("\n");
}
}
void judgeBinaryImageInfo(QString imagePath)
{
//cv::Mat image = cv::imread(imagePath.toStdString(), cv::IMREAD_GRAYSCALE); // 加载图像
QImage image = QImage(imagePath);
qDebug()<<"image.colorCount="<<image.colorCount();
qDebug()<<"image.format="<<image.format();
cv::Mat mat = cv::imread(imagePath.toStdString()); // 加载图像
qDebug()<<"mat.type="<<mat.type();
if (isGrayImage(mat)) {
std::cout << "The image is grayscale." << std::endl;
} else {
std::cout << "The image is not grayscale." << std::endl;
return;
}
cv::imshow("gray Image", mat);
cv::Mat mats[3];
split(mat,mats);
cv::imshow("gray gray Image", mat);
//cv::imshow("gray gray Image", mats[0]);
mat = mats[0];
int uniqueColors = cv::countNonZero(mat);
qDebug()<<"uniqueColors="<<uniqueColors;
qDebug()<<"mat.type="<<mat.type();
if(mat.type() == 0