- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的任务是制作自己的 Sobel 方法,而不是使用 OpenCV 中的 cv::Sobel
。我尝试实现我在 Programming techniques 找到的一个
但是,当我运行该程序时,cv::Mat
会抛出一个错误。有人知道为什么吗?
索贝尔法:
int sobelCorrelation(Mat InputArray, int x, int y, String xory)
{
if (xory == "x") {
return InputArray.at<uchar>(y - 1, x - 1) +
2 * InputArray.at<uchar>(y, x - 1) +
InputArray.at<uchar>(y + 1, x - 1) -
InputArray.at<uchar>(y - 1, x + 1) -
2 * InputArray.at<uchar>(y, x + 1) -
InputArray.at<uchar>(y + 1, x + 1);
}
else if (xory == "y")
{
return InputArray.at<uchar>(y - 1, x - 1) +
2 * InputArray.at<uchar>(y - 1, x) +
InputArray.at<uchar>(y - 1, x + 1) -
InputArray.at<uchar>(y + 1, x - 1) -
2 * InputArray.at<uchar>(y + 1, x) -
InputArray.at<uchar>(y + 1, x + 1);
}
else
{
return 0;
}
}
在另一个函数中调用并处理:
void imageOutput(Mat image, String path) {
image = imread(path, 0);
Mat dst;
dst = image.clone();
int sum, gx, gy;
if (image.data && !image.empty()){
for (int y = 0; y < image.rows; y++)
for (int x = 0; x < image.cols; x++)
dst.at<uchar>(y, x) = 0.0;
for (int y = 1; y < image.rows - 1; ++y) {
for (int x = 1; x < image.cols - 1; ++x){
gx = sobelCorrelation(image, x, y, "x");
gy = sobelCorrelation(image, x, y, "y");
sum = absVal(gx) + absVal(gy);
if (sum > 255)
sum = 255;
else if (sum < 0)
sum = 0;
dst.at<uchar>(x, y) = sum;
}
}
namedWindow("Original");
imshow("Original", image);
namedWindow("Diagonal Edges");
imshow("Diagonal Edges", dst);
}
waitKey(0);
}
主要内容:
int main(int argc, char* argv[]) {
Mat image;
imageOutput(image, "C:/Dropbox/2-falling-toast-ted-kinsman.jpg");
return 0;
}
absVal 方法:
int absVal(int v)
{
return v*((v < 0)*(-1) + (v > 0));
}
运行时会抛出这个错误:
Unhandled exception at 0x00007FFC9365A1C8 in Miniproject01.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000A780A4F110.
并指向这里:
template<typename _Tp> inline
_Tp& Mat::at(int i0, int i1)
{
CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] &&
(unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()) &&
CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
return ((_Tp*)(data + step.p[0] * i0))[i1];
}
如果有人对我做错了什么有任何建议或想法,我们将不胜感激!
最佳答案
此代码片段用于演示如何计算 Sobel 3x3 导数,将图像与 Sobel 内核进行卷积。您可以轻松地扩展到不同的内核大小,将内核半径作为 my_sobel
的输入,并创建适当的内核。
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
void my_sobel(const Mat1b& src, Mat1s& dst, int direction)
{
Mat1s kernel;
int radius = 0;
// Create the kernel
if (direction == 0)
{
// Sobel 3x3 X kernel
kernel = (Mat1s(3,3) << -1, 0, +1, -2, 0, +2, -1, 0, +1);
radius = 1;
}
else
{
// Sobel 3x3 Y kernel
kernel = (Mat1s(3, 3) << -1, -2, -1, 0, 0, 0, +1, +2, +1);
radius = 1;
}
// Handle border issues
Mat1b _src;
copyMakeBorder(src, _src, radius, radius, radius, radius, BORDER_REFLECT101);
// Create output matrix
dst.create(src.rows, src.cols);
// Convolution loop
// Iterate on image
for (int r = radius; r < _src.rows - radius; ++r)
{
for (int c = radius; c < _src.cols - radius; ++c)
{
short s = 0;
// Iterate on kernel
for (int i = -radius; i <= radius; ++i)
{
for (int j = -radius; j <= radius; ++j)
{
s += _src(r + i, c + j) * kernel(i + radius, j + radius);
}
}
dst(r - radius, c - radius) = s;
}
}
}
int main(void)
{
Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
// Compute custom Sobel 3x3 derivatives
Mat1s sx, sy;
my_sobel(img, sx, 0);
my_sobel(img, sy, 1);
// Edges L1 norm
Mat1b edges_L1;
absdiff(sx, sy, edges_L1);
// Check results against OpenCV
Mat1s cvsx,cvsy;
Sobel(img, cvsx, CV_16S, 1, 0);
Sobel(img, cvsy, CV_16S, 0, 1);
Mat1b cvedges_L1;
absdiff(cvsx, cvsy, cvedges_L1);
Mat diff_L1;
absdiff(edges_L1, cvedges_L1, diff_L1);
cout << "Number of different pixels: " << countNonZero(diff_L1) << endl;
return 0;
}
关于c++ - OpenCV 中的 Sobel 导数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32972513/
我尝试用java实现Sobel边缘检测。它有点有效,但我收到了很多看似随机的噪音...... 我将图像加载为 BufferedImages 并首先将其转换为灰度图像(通过我在网上找到的算法)。之后我计
我尝试在 Java 中实现 Sobel 运算符,但结果只是一些像素的混合。 int i, j; FileInputStream inFile = new FileInputStream
我有每个像素的 Sobel 算子的梯度。在我的例子中是 320x480。但是我怎样才能将它们与方向联系起来呢?例如,我打算绘制指纹方向图。那么,我该如何开始呢? 是将梯度分成 block (例如 16
我为 Sobel 运算符编写了一个用于边缘检测的类,但是当我使用示例图像时,我的边缘消失了。如果有人可以帮助我,我将不胜感激。 import java.awt.image.BufferedImage;
x-导数 Sobel 看起来是这样的: -1 0 +1 -2 0 +2 -1 0 +1 假设我的图像有两个样本看起来像这样(0=黑色,1=白色): 0 0 1 1 0 0 0 0
我正在使用图像上的索贝尔边缘检测进行作业。我目前正在努力进行渐变操作。编译时收到“二元运算符 '*' 的操作数类型错误”错误。我认为这可能是因为我将所有像素定义为字母,并且我不确定下一步应该是什么。任
我的 Sobel 边缘检测算子的输出很奇怪。这是我的代码: BufferedImage temp = img; float kernelx[][] = {{-1, 0, 1},{-2,
我想在我的 android 应用程序中使用 Sobel 运算符。但是我不明白如何使用一个像素。 int sobel_x[][] = {{-1, 0, 1}, {-2, 0, 2},
我想在图像上使用高斯模糊作为使用 sobel 边缘检测过滤器的预处理步骤。 我以前在灰度图像上有效地实现了 sobel 和高斯模糊运算符,但是,我从未尝试过在彩色图像上使用它们。 之前,我一直采用像素
对于嵌入式设计,我试图在不使用缓冲器的情况下在板上实现索贝尔边缘检测。即我直接从屏幕上阅读和写作。但是,我可以存储大约一两个图像宽度的数据以供以后引用。这是由于董事会规定的限制。但是我陷入了一些问题。
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我试图理解 cv2.Sobel 中的 scale 参数.将 scale 设置为 1/8,我沿 x 轴得到如下输出: 但是对于 scale = 10 或 scale = 100,输出非常相似。 上面两幅
我正在使用 sobel 运算符通过 EmguCV 3.0(OpenCV 的 .NET 包装器)检测灰度图像中的边缘。 我使用的代码是 Image gray = new Image(@"C:\gray.
我正在使用 skimage 创建一个与此类似的 sobel 过滤器图像... 我想知道有没有办法锐化这个 sobel 滤镜图像?比如说,把那些比较淡的白线去掉,比如气球后面的淡淡的线条? 我用 Ski
OpenCV 文档说,(order == 0) 表示不会在这个方向上应用导数,即不会执行此内核的计算。 (Order == 1) 意味着这个方向的图像和内核只是一个简单的卷积。 但是 (order =
我想在文本中找到笔划的方向。 Sobel 算子如何用于此目的? 这张图显示的是dp,也就是梯度方向。我想知道如何应用 Sobel 运算符找到要选择的像素,从 p 到 q,沿着路径 sp,到找到边缘上的
我正在尝试对墙壁图像使用 sobel 过滤器,但它不起作用。 我的代码是: im=scipy.misc.imread('IMG_1479bis.JPG') im = im.astype('int32'
阅读一篇论文,我很难理解所描述的算法: 给定手写样本的黑白数字图像,裁剪出单个字符进行分析。由于这可以是任意大小,因此算法需要考虑到这一点(如果更简单,我们可以假设大小为 2^n x 2^m)。 现在
我正在尝试在水平和垂直方向上实现 sobel 运算符。但不知何故我得到了反向输出。我在下面附上的代码。对于水平蒙版 char mask [3][3]= {{-1,-2,-1},{0,0,0},{1,
我在使用 Sobel 算子进行边缘检测时遇到问题:它会产生太多假边缘,效果如下图所示。我正在使用 3x3 sobel 运算符 - 首先提取垂直然后水平,最终输出是每个滤波器输出的幅度。合成图像的边
我是一名优秀的程序员,十分优秀!