cv::Mat::zeros(img.size(),CV_8UC3);
时间: 2024-06-01 22:10:25 浏览: 118
This line of code creates a new `cv::Mat` object with dimensions equal to `img` and initializes all its values to zero. The `CV_8UC3` parameter specifies the data type of the matrix as an 8-bit unsigned integer with 3 channels (corresponding to the Red, Green, and Blue color channels). This can be useful for creating a blank image of the same size and color format as another image, which can then be used as a canvas for drawing or processing operations.
相关问题
Mat gray = img, gray2 = gray; Cv2.CvtColor(img, gray, ColorConversionCodes.BGR2GRAY); Mat gray3 = Cv2.ImRead("D:\\test.png", ImreadModes.Grayscale); hist = img; Mat hist3 = hist; int[] channels = { 0 }; Rangef[] inRanges = new Rangef[] { new Rangef(0, 256) }; int[] histSize = { 256 }; Mat[] grays = new Mat[] { gray }; Mat[] grays3 = new Mat[] { gray3 }; Cv2.CalcHist(grays, channels, new Mat(), hist, 1, histSize, inRanges); Cv2.CalcHist(grays3, channels, new Mat(), hist3, 1, histSize, inRanges); Mat histImage = Mat.Zeros(400, 512, MatType.CV_8UC3);
这段代码使用了OpenCV库对图像进行处理和直方图计算。首先将读入的彩色图像转换为灰度图像,使用Cv2.CvtColor函数实现。然后使用Cv2.ImRead函数读取另一张灰度图像。接下来使用Cv2.CalcHist函数计算两张灰度图像的直方图,其中hist和hist3是存储直方图的Mat变量。channel表示通道数,由于是灰度图像,所以只有一个通道。inRanges表示像素值范围,这里是0-255。histSize表示直方图的区间数,这里是256。grays和grays3是要计算直方图的灰度图像数组。最后使用Mat.Zeros函数创建一个大小为400x512的黑色Mat变量作为直方图图像的背景,它的类型为CV_8UC3。
import numpy as np import cv2 as cv # 加载图片 img = cv.imread('4.jpg') # 灰度化 img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # 二值化 ret, thresh = cv.threshold(img_gray, 127, 255, cv.THRESH_BINARY) # 寻找连通域 num_labels, labels, stats, centroids = cv.connectedComponentsWithStats(thresh, connectivity=8) # 计算平均面积 areas = list() for i in range(num_labels): areas.append(stats[i][-1]) print("轮廓%d的面积:%d" % (i, stats[i][-1])) area_avg = np.average(areas[1:-1]) print("轮廓平均面积:", area_avg) # 筛选超过平均面积的连通域 image_filtered = np.zeros_like(img) for (i, label) in enumerate(np.unique(labels)): # 如果是背景,忽略 if label == 0: continue if stats[i][-1] < area_avg : image_filtered[labels == i] = 255 #cv.imshow("image_filtered", image_filtered) #cv.imshow("img", img) cv.imwrite('4.jpg',image_filtered ) cv.waitKey() cv.destroyAllWindows(),将上述代码转化为c++形式
以下是将上述Python代码转化为C++形式的示例:
```cpp
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
// 加载图片
Mat img = imread("4.jpg");
// 灰度化
Mat img_gray;
cvtColor(img, img_gray, COLOR_BGR2GRAY);
// 二值化
Mat thresh;
threshold(img_gray, thresh, 127, 255, THRESH_BINARY);
// 寻找连通域
Mat labels, stats, centroids;
int num_labels = connectedComponentsWithStats(thresh, labels, stats, centroids, 8);
// 计算平均面积
vector<int> areas;
for (int i = 0; i < num_labels; i++) {
areas.push_back(stats.at<int>(i, CC_STAT_AREA));
cout << "轮廓" << i << "的面积:" << stats.at<int>(i, CC_STAT_AREA) << endl;
}
double area_avg = mean(Mat(areas))[0];
cout << "轮廓平均面积:" << area_avg << endl;
// 筛选超过平均面积的连通域
Mat image_filtered = Mat::zeros(img.size(), CV_8UC1);
for (int i = 1; i < num_labels; i++) {
if (stats.at<int>(i, CC_STAT_AREA) < area_avg) {
labels.setTo(0, labels == i);
} else {
image_filtered.setTo(255, labels == i);
}
}
// 显示结果
imshow("image_filtered", image_filtered);
imshow("img", img);
waitKey();
destroyAllWindows();
// 保存结果
imwrite("4.jpg", image_filtered);
return 0;
}
```
需要注意的是,C++中没有Python中的`list`类型,需要使用`vector`替代。同时,C++中没有Python中的`np.average()`方法,需要使用OpenCV的`mean()`方法计算平均值。此外,C++中的Mat类需要使用`.at()`方法访问元素,而不是Python中的下标索引。
阅读全文