gpt4 book ai didi

c++ - 在 x 方向和 y 方向计算一次 1st Sobel 导数并将这两者结合起来(对于每个 channel )

转载 作者:太空宇宙 更新时间:2023-11-03 22:52:40 25 4
gpt4 key购买 nike

我需要一些帮助来解决我现在遇到的这个问题。我有 3 张图像,它们只是不同之处相同,它们代表蓝色、绿色和红色。我需要将它们组合起来并得到彩色图像。我正在使用 opencv 和 c++,但现在我遇到了这个问题,我无法弄清楚。

需要:进行边缘检测。

----更新----我写了一些新代码

  Sobel(img_r, x, CV_16S, 1, 0);
Sobel(img_r, y, CV_16S, 0, 1);
//Compute the L1 norm
sobel_L1_norm = abs(x)+abs(y);
//Find Sobel max value
minMaxLoc(sobel_L1_norm, &min, &max);
sobel_L1_norm.convertTo(sobel_image, CV_32F, 255.0/(max - min), -min * 255.0/(max - min));
threshold(sobel_image, edgeThreshold, min, 255, THRESH_BINARY);
edgeThreshold.copyTo(img_r_edge);

我得到这个结果 bad example

不过应该​​是这样的。 correct one

-----完整代码-----

Mat img_r = imread(input_path + "/01.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat img_g = imread(input_path + "/02.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat img_b = imread(input_path + "/03.png", CV_LOAD_IMAGE_GRAYSCALE);
// Edge Images
Mat img_r_edge = Mat::zeros(img_r.size(), CV_8UC1);
Mat img_g_edge = Mat::zeros(img_g.size(), CV_8UC1);
Mat img_b_edge = Mat::zeros(img_b.size(), CV_8UC1);

std::cout << "Step 1 - calculating edge images... ";
// TODO: 1) Calculate the 1st Sobel derivative once in x and once in y direction and combine these two
// (for every channel).
Mat x;
Mat y;
Mat abs_x;
Mat abs_y;
Mat sobel_L1_norm;
Mat sobel_image;
Mat edgeThreshold;
double min, max; //Finding min and max Sobel valuye;


//---------------------------------------------------

Sobel(img_r, x, CV_16S, 1, 0);
Sobel(img_r, y, CV_16S, 0, 1);
//Compute the L1 norm
sobel_L1_norm = abs(x)+abs(y);
//Find Sobel max value
minMaxLoc(sobel_L1_norm, &min, &max);
sobel_L1_norm.convertTo(sobel_image, CV_32F, 255.0/(max - min), -min * 255.0/(max - min));
threshold(sobel_image, edgeThreshold, min, 255, THRESH_BINARY);
edgeThreshold.copyTo(img_r_edge);

//----------------------------------------------------


// 2) Normalize every gradient image and convert the results to CV_8UC1.
// 3) Threshold the retrieved (normalized) gradient images using the parameter "edgeThreshold".
// 4) Save the results in the cv::Mats below.

imwrite(out_r_edge_filename, sobel);
imwrite(out_g_edge_filename, img_g_edge);
imwrite(out_b_edge_filename, img_b_edge);

最佳答案

您正在设定 sobel_image 的阈值有阈值min .

但是min将(几乎)永远是 0 ,因为它是 sobel_L1_norm 的最小值图像。请注意,没有渐变的像素的值为 0。在sobel_L1_norm .

解决这个问题的方法是为阈值选择一个有意义的值。由于您将值标准化为在 [0, 255] 范围内,您可以选择该范围内的一个值(大于 0)。

如果您使用 [0,1] 中的值进行标准化, 在这个区间内取一个值。


您可以使用 normalize(..., NORM_MINMAX)而不是找到最大值并重新缩放。


还要注意 edgeThreshold将是 CV_32F 类型的矩阵在调用 threshold 后, 所以它也是 img_r_edge .使用 imwrite 正确保存图像, 枯萎使用 CV_32F图片在 [0,1] 范围内, 或 CV_8U [0,255] 范围内的图像。所以你需要重新缩放 img_r_edge在范围 [0,1] , 或将其转换为 CV_8U .


您在这里混合了很多 OpenCV 类型。 Mat_<Tp> 通常更容易使用确切地知道类型。

您可以随时使用 CV_32F图片,范围在 [0,1]。


将产生正确输出的代码,以及建议的修改:

#include <opencv2/opencv.hpp>
#include <vector>
using namespace cv;
using namespace std;


int main()
{
Mat3b img = imread(path_to_color_image");

vector<Mat1b> planes;
split(img, planes);

Mat1b img_r = planes[2].clone();
Mat1b img_g = planes[1].clone();
Mat1b img_b = planes[0].clone();

// Edge Images
Mat1b img_r_edge;
Mat1b img_g_edge;
Mat1b img_b_edge;

// TODO: 1) Calculate the 1st Sobel derivative once in x and once in y direction and combine these two
// (for every channel).
Mat1f dx, dy;
Mat1f sobel_L1_norm;
Mat1f sobel_image;
Mat1f edgeThreshold;
double min, max; //Finding min and max Sobel valuye;


//---------------------------------------------------

Sobel(img_r, dx, CV_32F, 1, 0);
Sobel(img_r, dy, CV_32F, 0, 1);

//Compute the L1 norm
sobel_L1_norm = abs(dx) + abs(dy); // Type

// Normalize
normalize(sobel_L1_norm, sobel_image, 0, 1, NORM_MINMAX);

// Use a value different from 'min', which will (almost always) be 0.
double thresh = 0.5;
threshold(sobel_image, edgeThreshold, thresh, 255, THRESH_BINARY);

edgeThreshold.convertTo(img_r_edge, CV_8U);
imwrite("img_r_edge.png", img_r_edge);


return 0;
}

关于c++ - 在 x 方向和 y 方向计算一次 1st Sobel 导数并将这两者结合起来(对于每个 channel ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36155267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com