1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
// I have 3 problems now
//1. after playing with the code, i found that i didnt get the edges' coordinates,
// in fact, i am only getting the pixel coordinates that are near to the edges + the
//coordinates around rotating table
// can anybody guide me how should i do to get only the object's edges' pixel coordinates?
//2. I was going to make a little changes on some pixel coordinates, then rewrite an image,
//but i got some assertion error that i dont have on another project i had.
//3. I am not even sure if I am storing the pixels right, because i have problem in displaying
//them out.
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
#include <vector>
#include <direct.h>
#include <algorithm> // for copy
#include <iterator> // for ostream_iterator
using namespace cv;
using namespace std;
Mat pic, gray;
int rows, cols;
string path_in = "C:/Users/Dell/Desktop/";
string path_out = "C:/Users/Dell/Desktop/New Folder/";
int main(int argc, char** argv)
{
vector<cv::String> fn;
vector<cv::String> fo;
glob(path_in + "*.jpg", fn, false);
glob(path_out + "*.jpg", fo, false);
size_t count = fn.size();
for (size_t i = 1; i < 2; i++) //try for an image first, if works, then try for all images in folder
{
pic = imread(fn[i], CV_LOAD_IMAGE_COLOR);
namedWindow("Before", 1);
cv::imshow("Before", pic);
cvtColor(pic, gray, CV_BGR2GRAY);
cout << fn[i] << endl;
cout << pic.rows << ", " << pic.cols << endl; //480, 640
//int arry[][] = {};
//myVec.reserve(dimension * dimension);
std::vector<Point2d> myVec;
for (int r = 0; r < gray.rows; r++) {
for (int c = 0; c < gray.cols; c++) {
if (gray.at<uchar>(r, c) > 0) { //!=black
//if (gray.at<uchar>(r, c) == 0) { //if it's black,
//cout << r << ", " << c << endl;
//cin >> arry[r][c];
//gray.at<uchar>(r, c) = 255;
myVec.push_back(Point2d(r, c));
//cout << Point2d(r, c) << endl;
continue;
}
break;
}
}
for (size_t k = gray.rows - 1 ; k != (size_t)-1; k--) {
for (size_t l = gray.cols - 1; l != (size_t)-1; l--) {
if (gray.at<uchar>(k, l) > 0) { //!=black
//if (gray.at<uchar>(k, l) == 0) { //if it's black,
//gray.at<uchar>(k, l) = 255; //paint to white
//that's how i found it didnt take the edges, but those coordinates near to edges
//guess that , other than edges, some pixels are not black too
myVec.push_back(Point2d(k, l));
continue;
}
break;
}
}
std::copy(myVec.begin(), myVec.end(), std::ostream_iterator<Point2d>(std::cout, " "));
//no error, but it didnt cout my vector output
imshow("After", gray);
waitKey(0);
imwrite(fo[i], gray); //Assertion error. not working:( no image saved in output folder
}
return 0;
}
//waitKey(0) not working,
//http://blog.csdn.net/u010963435/article/details/78658397
|