想用Opencv提取图片中像素点不为0的坐标值

lwx0315 2017-04-18 10:56:18
已经得到了一副经过边缘检测的图片
想要获得图中像素点不为0的所有边缘像素的坐标值,并将这些坐标值存入数组中,以便后续操作,请问可以怎么做呀?
...全文
1508 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lwx0315 2017-04-18
  • 打赏
  • 举报
回复
找老师您这个是Opencv吗?我是小白,刚开始接触,很多东西还不懂@赵4老师
赵4老师 2017-04-18
  • 打赏
  • 举报
回复
仅供参考:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;

int main() {
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);

    wstring infilename(L"1.jpg");
    string outfilename("color.txt");

    Bitmap* bmp = new Bitmap(infilename.c_str());
    UINT height = bmp->GetHeight();
    UINT width  = bmp->GetWidth();
    cout << "width " << width << ", height " << height << endl;

    Color color;
    ofstream fout(outfilename.c_str());

    for (UINT y = 0; y < height; y++)
    for (UINT x = 0; x < width ; x++) {
            bmp->GetPixel(x, y, &color);
            fout << x << "," << y << ";"
                 << (int)color.GetRed()   << ","
                 << (int)color.GetGreen() << ","
                 << (int)color.GetBlue()  << endl;
    }

    fout.close();

    delete bmp;
    GdiplusShutdown(gdiplustoken);
    return 0;
}
lwx0315 2017-04-18
  • 打赏
  • 举报
回复
@赵4老师 赵4老师,求教~
lwx0315 2017-04-18
  • 打赏
  • 举报
回复
我用opencv写了这样一段代码,编译的时候没有出错,但是一运行就报错,不知道该怎么办了
//提取检测到的边缘坐标值//

//动态分配数组
int *y = (int *)malloc(img6.cols*sizeof(int)); //存储行坐标
int *x = (int *)malloc(img6.cols*sizeof(int)); //存储列坐标

memset(x, 0, sizeof(x)); //初始化为0
memset(y, 0, sizeof(y));

int count = 0;

for(int i=0; i<img6.rows; ++i)
{
for(int j=0; j<img6.cols; ++j)
{
if(img6.at<uchar>(i,j) != 0)
{
x[count] = j; //拿到检测到的边缘的列坐标
y[count] = i; //拿到检测到的边缘的行坐标
count ++;
}
}
}

free(x);
free(y);
lwx0315 2017-04-18
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
3楼代码是调用GDI+读取图片文件的每个像素对应的RGB值。
  int count=0;
  for (int y=0; y<img6.rows; ++y) {
  for (int x=0; x<img6.cols; ++x) {
    if (img6.at<uchar>(y,x) > 30) count++;
  }}
  int *yy = (int *)malloc(count*sizeof(int));
  int *xx = (int *)malloc(count*sizeof(int));
  int i=0;
  for (int y=0; y<img6.rows; ++y) {
  for (int x=0; x<img6.cols; ++x) {
    if (img6.at<uchar>(y,x) > 30) {
      yy[i]=y;
      xx[i]=x;
      i++;
    }
  }}
  //...
  free(xx);
  free(yy);
太感谢赵老师了,初学者这个问题困扰一周了,现在解决啦~嘻嘻
赵4老师 2017-04-18
  • 打赏
  • 举报
回复
3楼代码是调用GDI+读取图片文件的每个像素对应的RGB值。
  int count=0;
  for (int y=0; y<img6.rows; ++y) {
  for (int x=0; x<img6.cols; ++x) {
    if (img6.at<uchar>(y,x) > 30) count++;
  }}
  int *yy = (int *)malloc(count*sizeof(int));
  int *xx = (int *)malloc(count*sizeof(int));
  int i=0;
  for (int y=0; y<img6.rows; ++y) {
  for (int x=0; x<img6.cols; ++x) {
    if (img6.at<uchar>(y,x) > 30) {
      yy[i]=y;
      xx[i]=x;
      i++;
    }
  }}
  //...
  free(xx);
  free(yy);

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧