openCV提取图像边缘像素坐标点

qq_38089780 2017-07-20 04:20:51
#include "stdafx.h"
#include <iostream>
#include "cv.h"
#include <highgui.h>
#include<fstream>

using namespace std;

int main()
{
IplImage *img = cvLoadImage("E:\\7.bmp", CV_LOAD_IMAGE_COLOR);
uchar *data = (uchar *)img->imageData;
int step = img->widthStep / sizeof(uchar);
int channels = img->nChannels;
int R, G, B;
ofstream fout;
fout.open("E:\\RGB.txt", ios::app);
for (int i = 0; i < img->height; i++)
{
for (int j = 0; j < img->width; j++)
{
B = (int)data[i * step + j * channels + 0];
G = (int)data[i * step + j * channels + 1];
R = (int)data[i * step + j * channels + 2];

fout << "( " << i << ", " << j << " ) = ( " << R << "," << G << "," << B << ")" << endl;


}
}
fout.close();
return 0;
}

上面这个程序能提取所有坐标的RGB值并保存在txt当中,我想实现当 R=G=B值为零时不保存,只保存非零时的坐标值,请问应该如何修改。真心请教各位大神了。



或者有没有程序能直接将图片中像素边缘点的坐标提取并保存到txt中。
...全文
3314 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-07-26
  • 打赏
  • 举报
回复
仅供参考:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
using namespace std;
using namespace cv;
Mat img,smallImg,gray,bw;
vector<Vec4i> hierarchy;
vector<vector<Point> > contours;
int threshval=128;
Rect r;
Rect maxrect,brect;
int idx,n;
const static Scalar colors[15]={
    CV_RGB(  0,  0,128),
    CV_RGB(  0,128,  0),
    CV_RGB(  0,128,128),
    CV_RGB(128,  0,  0),
    CV_RGB(128,  0,128),
    CV_RGB(128,128,  0),
    CV_RGB(128,128,128),
    CV_RGB(160,160,160),
    CV_RGB(  0,  0,255),
    CV_RGB(  0,255,  0),
    CV_RGB(  0,255,255),
    CV_RGB(255,  0,  0),
    CV_RGB(255,  0,255),
    CV_RGB(255,255,  0),
    CV_RGB(255,255,255),
};
Scalar color;
void gamma_correct(Mat& img, Mat& dst, double gamma) {
	Mat	temp;
	CvMat tmp;

	img.convertTo(temp,	CV_32FC1, 1.0/255.0, 0.0);
	tmp=temp;
	cvPow(&tmp,	&tmp, gamma);
	temp.convertTo(dst , CV_8UC1 , 255.0	, 0.0);
}
int main() {
    cvNamedWindow("display",1);
    img=imread("image.jpg",1);
    r.x=img.cols/10;
    r.y=img.rows/3;
    r.width=img.cols*8/10;
    r.height=img.rows*2/3;
    smallImg=img(r);
    cvtColor(smallImg,gray,CV_BGR2GRAY);
//  medianBlur(gray,gray,5);
    equalizeHist(gray,gray);
    gamma_correct(gray,gray,4.0);
    imshow("display",gray);
    waitKey(0);

	bw=(gray>threshval);
    imshow("display",bw);
    waitKey(0);

	Mat	Structure0=getStructuringElement(MORPH_RECT,Size(3,3));
    erode(bw,bw,Structure0,Point(-1,-1));
	Mat	Structure1=getStructuringElement(MORPH_RECT,Size(6,6));
    dilate(bw,bw,Structure1, Point(-1,-1));
    imshow("display",bw);
    waitKey(0);

    findContours(bw,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
    if (!contours.empty()&&!hierarchy.empty()) {
        idx=0;
        n=0;
        vector<Point> approx;
        for (;idx>=0;idx=hierarchy[idx][0]) {
            color=colors[idx%15];
//          drawContours(smallImg,contours,idx,color,1,8,hierarchy);
            approxPolyDP(Mat(contours[idx]), approx, arcLength(Mat(contours[idx]), true)*0.005, true);//0.005为将毛边拉直的系数
            const Point* p = &approx[0];
            int m=(int)approx.size();
            polylines(smallImg, &p, &m, 1, true, color);
            circle(smallImg,Point(p[0].x,p[0].y),2,color);
            circle(smallImg,Point(p[1].x,p[1].y),1,color);

            n++;
            if (1==n) {
                maxrect=boundingRect(Mat(contours[idx]));
            } else {
                brect=boundingRect(Mat(contours[idx]));
                CvRect mr(maxrect),br(brect);
                maxrect=cvMaxRect(&mr,&br);
            }
        }
        circle(smallImg,Point(maxrect.x+maxrect.width/2,maxrect.y+maxrect.height/2),2,CV_RGB(255,0,0));
    }
    imshow("display",smallImg);
    waitKey(0);
    cvDestroyWindow("display");
    return 0;
}
qq_38089780 2017-07-25
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
#include "stdafx.h"
#include <iostream>
#include "cv.h"
#include <highgui.h>
#include <fstream>
using namespace std;
int main() {
    IplImage *img = cvLoadImage("E:\\7.bmp", CV_LOAD_IMAGE_COLOR);
    uchar *data = (uchar *)img->imageData;
    int step = img->widthStep / sizeof(uchar);
    int channels = img->nChannels;
    int R, G, B;
    ofstream fout;
    fout.open("E:\\RGB.txt", ios::app);
    for (int i = 0; i < img->height; i++) {
        for (int j = 0; j < img->width; j++) {
            B = (int)data[i * step + j * channels + 0];
            G = (int)data[i * step + j * channels + 1];
            R = (int)data[i * step + j * channels + 2];
            if (!(R==0 && G==0 && B==0)) //当R=G=B值为零时不保存
                fout << "( " << i << ", " << j << " ) = ( " << R << "," << G << "," << B << ")" << endl;
        }
    }
    fout.close();
    return 0;
}
谢谢赵老师,这个问题我解决了。因为我刚接触,所以很多方面都不太懂。想请教您一下我对一张图片提取边缘后获得了一些比较离散的点还有线,但是大致轮廓是有的。如何将该轮廓拟合成一条闭合的曲线? 请问有什么大体的方向么?谢谢您了。
赵4老师 2017-07-21
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
#include "cv.h"
#include <highgui.h>
#include <fstream>
using namespace std;
int main() {
    IplImage *img = cvLoadImage("E:\\7.bmp", CV_LOAD_IMAGE_COLOR);
    uchar *data = (uchar *)img->imageData;
    int step = img->widthStep / sizeof(uchar);
    int channels = img->nChannels;
    int R, G, B;
    ofstream fout;
    fout.open("E:\\RGB.txt", ios::app);
    for (int i = 0; i < img->height; i++) {
        for (int j = 0; j < img->width; j++) {
            B = (int)data[i * step + j * channels + 0];
            G = (int)data[i * step + j * channels + 1];
            R = (int)data[i * step + j * channels + 2];
            if (!(R==0 && G==0 && B==0)) //当R=G=B值为零时不保存
                fout << "( " << i << ", " << j << " ) = ( " << R << "," << G << "," << B << ")" << endl;
        }
    }
    fout.close();
    return 0;
}
沐阳2100 2017-07-21
  • 打赏
  • 举报
回复
既然楼主RGB的值都提出来了,添加如楼上赵四老师的判断不就行了?难道我们理解有误,不是这个问题?
qq_38089780 2017-07-20
  • 打赏
  • 举报
回复
这是经处理得到的边缘图

19,468

社区成员

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

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