cv2.CC_STAT_AREA与cv2.contourArea的区别
时间: 2024-04-09 14:30:57 浏览: 208
cv2.CC_STAT_AREA和cv2.contourArea是OpenCV中与轮廓相关的函数。
cv2.CC_STAT_AREA是在使用cv2.connectedComponentsWithStats函数进行连通组件分析时返回的一个统计信息数组中的一个元素。它表示连通组件的面积,即像素的数量。
cv2.contourArea是用于计算给定轮廓的面积的函数。它接受一个轮廓作为参数,并返回该轮廓的面积。轮廓可以通过cv2.findContours函数获得。
所以区别在于,cv2.CC_STAT_AREA返回的是连通组件的面积,而cv2.contourArea返回的是给定轮廓的面积。
相关问题
将以下的python程序转化为c++版本,并在vs2022上实现。import cv2 import numpy as np from skimage.transform import radon import os thre1=10 thre2=-10 r=60 maxVal = 0 index = 0 sequence_path = "./images/" for file in os.listdir(sequence_path): filename=os.path.join(sequence_path, file) image=cv2.imread(filename, 0) image=cv2.blur(image,(3,3)) img=np.zeros((len(image), len(image[0])),np.uint8) maxVal = 0 index = 0 retval, labels, stats, centroids = cv2.connectedComponentsWithStats(img, connectivity=8) for i in range(1, len(stats)): if stats[i][4] > maxVal: maxVal = stats[i][4] index = i #x,y,h,w s for i in range(len(labels)): for j in range(len(labels[0])): if labels[i][j]==index: labels[i][j]=255 else: labels[i][j] = 0 cv2.imwrite('./4-max_region.jpg',labels) img2=cv2.imread('./4-max_region.jpg',0) img3=cv2.Canny(img2,15,200) # theta = np.linspace(0, 180, endpoint=False) img4 = radon(img3) max_angel=0 for i in range(len(img4)): for j in range(len(img4[0])): if img4[i][j]>max_angel: max_angel=img4[i][j] angel=j print("{}方向为:{} °".format(filename,angel))
在VS2022上实现这段Python代码的C++版本,你可以使用以下代码作为参考:
```cpp
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/imgcodecs.hpp>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
using namespace cv;
int main() {
int thre1 = 10;
int thre2 = -10;
int r = 60;
int maxVal = 0;
int index = 0;
string sequence_path = "./images/";
for (const auto &entry : filesystem::directory_iterator(sequence_path)) {
string filename = entry.path().string();
Mat image = imread(filename, IMREAD_GRAYSCALE);
blur(image, image, Size(3,3));
Mat img(image.rows, image.cols, CV_8U, Scalar(0));
maxVal = 0;
index = 0;
Mat labels, stats, centroids;
int retval = connectedComponentsWithStats(img, labels, stats, centroids, 8);
for (int i = 1; i < stats.rows; i++) {
if (stats.at<int>(i, CC_STAT_AREA) > maxVal) {
maxVal = stats.at<int>(i, CC_STAT_AREA);
index = i;
}
}
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
if (labels.at<int>(i, j) == index)
labels.at<int>(i, j) = 255;
else
labels.at<int>(i, j) = 0;
}
}
imwrite("./4-max_region.jpg", labels);
Mat img2 = imread("./4-max_region.jpg", IMREAD_GRAYSCALE);
Mat img3;
Canny(img2, img3, 15, 200);
Mat img4;
radon(img3, img4);
int max_angel = 0;
int angel = 0;
for (int i = 0; i < img4.rows; i++) {
for (int j = 0; j < img4.cols; j++) {
if (img4.at<float>(i, j) > max_angel) {
max_angel = img4.at<float>(i, j);
angel = j;
}
}
}
cout << filename << " 方向为:" << angel << " °" << endl;
}
return 0;
}
```
请注意,你需要在项目中添加OpenCV库的链接。此外,代码中使用了C++17的filesystem库进行文件遍历,因此你需要在编译时启用C++17标准。
将以下python代码转化为c++版本。import math import cv2 import numpy as np import os thre1=10 thre2=-10 r=60 ang =0 def select_point(image,ang): #根据遥杆方向确定跟踪点坐标 sinA=math.sin(ang) cosA=math.cos(ang) dirBaseX=int(cosA1000) disBaseY=int(-sinA1000) dirValMax=-1000000000 for i in range(len(image)): for j in range(len(image[0])): if image[i][j]==255: dirVal=idisBaseY+jdirBaseX if dirVal>dirValMax: rstRow=i rstCol=j dirValMax=dirVal return [rstCol,rstRow] sequence_path = "./images/" save_path="./out/" for file in os.listdir(sequence_path): filename=os.path.join(sequence_path, file) image=cv2.imread(filename, 0) image=cv2.blur(image,(3,3)) img=np.zeros((len(image), len(image[0])),np.uint8) for i in range(r,len(image)-r): for j in range(r,len(image[0])-r): shizi_1=( int(image[i][j])-int(image[i-r][j])>thre1 and int(image[i][j])-int(image[i][j-r])>thre1 and (int(image[i][j])-int(image[i+r][j])>thre1) and int(image[i][j])-int(image[i][j+r])>thre1 ) xieshizi_1=( int(image[i][j])-int(image[i-r][j-r])<thre2 and int(image[i][j])-int(image[i+r][j-r])<thre2 and int(image[i][j])-int(image[i-r][j+r])<thre2 and int(image[i][j])-int(image[i+r][j+r])<thre2 ) if (shizi_1 or xieshizi_1): img[i][j]=255 else: img[i][j] =0 retval, labels, stats, centroids = cv2.connectedComponentsWithStats(img, connectivity=8) maxVal = 0 index = 0 for i in range(1, len(stats)): if stats[i][4] > maxVal: maxVal = stats[i][4] index = i #x,y,h,w s for i in range(len(labels)): for j in range(len(labels[0])): if labels[i][j]==index: labels[i][j]=255 else: labels[i][j] = 0 img2=np.array(labels) target_x,target_y=select_point(img2,ang) print("跟踪点坐标:{}".format((target_x,target_y))) cv2.imwrite(os.path.join(save_path, file), cv2.circle(image,(int(target_x),int(target_y)),5,(255,255,0),2))
以下是将给定的Python代码转化为C++版本的示例:
```cpp
#include <iostream>
#include <cmath>
#include <opencv2/opencv.hpp>
int thre1 = 10;
int thre2 = -10;
int r = 60;
double ang = 0;
std::vector<int> select_point(cv::Mat image, double ang) {
double sinA = std::sin(ang);
double cosA = std::cos(ang);
int dirBaseX = static_cast<int>(cosA * 1000);
int disBaseY = static_cast<int>(-sinA * 1000);
int dirValMax = -1000000000;
int rstRow = 0, rstCol = 0;
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
if (image.at<uchar>(i, j) == 255) {
int dirVal = i * disBaseY + j * dirBaseX;
if (dirVal > dirValMax) {
rstRow = i;
rstCol = j;
dirValMax = dirVal;
}
}
}
}
return { rstCol, rstRow };
}
int main() {
std::string sequence_path = "./images/";
std::string save_path = "./out/";
for (const auto& file : std::filesystem::directory_iterator(sequence_path)) {
std::string filename = file.path().string();
cv::Mat image = cv::imread(filename, cv::IMREAD_GRAYSCALE);
cv::blur(image, image, cv::Size(3, 3));
cv::Mat img(image.rows, image.cols, CV_8UC1, cv::Scalar(0));
for (int i = r; i < image.rows - r; i++) {
for (int j = r; j < image.cols - r; j++) {
bool shizi_1 = (image.at<uchar>(i, j) - image.at<uchar>(i - r, j) > thre1 &&
image.at<uchar>(i, j) - image.at<uchar>(i, j - r) > thre1 &&
image.at<uchar>(i, j) - image.at<uchar>(i + r, j) > thre1 &&
image.at<uchar>(i, j) - image.at<uchar>(i, j + r) > thre1);
bool xieshizi_1 = (image.at<uchar>(i, j) - image.at<uchar>(i - r, j - r) < thre2 &&
image.at<uchar>(i, j) - image.at<uchar>(i + r, j - r) < thre2 &&
image.at<uchar>(i, j) - image.at<uchar>(i - r, j + r) < thre2 &&
image.at<uchar>(i, j) - image.at<uchar>(i + r, j + r) < thre2);
if (shizi_1 || xieshizi_1) {
img.at<uchar>(i, j) = 255;
}
else {
img.at<uchar>(i, j) = 0;
}
}
}
cv::Mat labels, stats, centroids;
int retval = cv::connectedComponentsWithStats(img, labels, stats, centroids, 8);
int maxVal = 0;
int index = 0;
for (int i = 1; i < stats.rows; i++) {
if (stats.at<int>(i, cv::CC_STAT_AREA) > maxVal) {
maxVal = stats.at<int>(i, cv::CC_STAT_AREA);
index = i;
}
}
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
if (labels.at<int>(i, j) == index) {
labels.at<int>(i, j) = 255;
}
else {
labels.at<int>(i, j) = 0;
}
}
}
cv::Mat img2(labels.rows, labels.cols, CV_8UC1);
labels.convertTo(img2, CV_8UC1);
std::vector<int> target = select_point(img2, ang);
int target_x = target[0];
int target_y = target[1];
std::cout << "跟踪点坐标:" << target_x << "," << target_y << std::endl;
cv::circle(image, cv::Point(target_x, target_y), 5, cv::Scalar(255, 255, 0), 2);
cv::imwrite(save_path + file.path().filename().string(), image);
}
return 0;
}
```
请注意,C++代码中的OpenCV库版本可能需要根据您的环境进行调整,并且可能需要包含适当的头文件和链接适当的库。此外,文件系统操作需要使用C++17标准。
阅读全文