赞
踩
- #include<iostream>
- #include<opencv2/core.hpp>
- #include<opencv2/highgui.hpp>
- #include<opencv2/imgproc.hpp>
- using namespace std;
- using namespace cv;
- int main()
- {
- Mat img = imread("C:\\Users\\Administrator\\Desktop\\1.bmp", 0);
- int rowNumber = img.rows;//行数
- int colNumber = img.cols*img.channels();//列数*通道数 =每一行元素的个数
- int *mean = new int[rowNumber];
-
-
- //双重循环,遍历所有的像素值
- for (int i = 0; i < rowNumber; i++)//行循环
- {
- int* data = img.ptr<int>(i);//获取第I行的首地址
- int hang = i;
- int meanCol=0;
- int sum = 0;
- int temp ;
- int *js = new int[colNumber];
- for (int j = 0; j < colNumber; j++)//列循环
- {
- js[j] = img.at<uchar>(i, j);
- sum += js[j];
- temp = j/2;//在这里求一个中位数 可以得到y
- }
- meanCol = sum / (temp*2);
- mean[i] = meanCol;
- //如果是白到黑输出白到黑的下标
- //如果是黑到白输出黑到白的下标
- if (i>0&&mean[i - 1]-mean[i]>0)//如果是白到黑 255-0 正数
- {
- int wb = i;
- printf("白到黑取出来的的点X值为:%d,Y值为:%d \n", temp, wb);
- //return wb;
- }
- else if (i>0&&mean[i - 1] - mean[i]<0)//如果是黑到白 0-255 负数
- {
- int bw = i-1;
- printf("黑到白取出来的的点X值为:%d,Y值为:%d \n", temp, bw);
- //return bw;
- }
- }
- system("pause");
- waitKey(0);
- return 0;
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。