OpenCV C++ issues

Hello good fellas!
I have two questions:
1. How to output all contours contains? If i set loop condition to 6 it gives segmentation error.
2. How can i output contours to text file without using constructor?
Need simple code to dump everything in text file

Namaste

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/imgproc/imgproc.hpp"  
#include <iostream>  
#include <stdio.h>  
#include <curses.h>
#include <iomanip>
  
using namespace cv;  
using namespace std;  
#define SX 20		
#define PATH "/home/supernova/Desktop/Resources/"		

void createInputVec(Mat_<float>,Mat_<int>);		
Mat trainPrePos(Mat);		

int main() {

	Mat_<float> featureVector(10,SX*SY); 
	Mat_<int> labelVector(1,10); 
	createInputVec(featureVector,labelVector); 
	
	Ptr<ml::KNearest>  knn(ml::KNearest::create());
	knn->train(featureVector, ml::ROW_SAMPLE, labelVector);	
			
	Mat img =imread("/home/supernova/Desktop/Resources/0.png");
	img = trainPrePos(img);
	img = img.reshape(1,1);
	Mat_<float> test(1,SX*SY);
	for(int i=0;i<1;++i) {
		test.at<float>(0,i)=float(img.at<uchar>(0,i));
	}
	
	float ans;
	Mat res,dist;
	ans=knn->findNearest(test, 1, noArray(),res,dist);
	std::cout << ans<< "\n";
	std::cout << dist ;

	getch();
	return 0;
}


void createInputVec(Mat_<float> features,Mat_<int> labels) {  
	 Mat img;  
	 char file[255];  
	
	 for (int j = 0; j < 2; j++)  {  
	
		  sprintf(file, "%s%d.png", PATH, j);  
		  
		  img = imread(file, 1);  
		 
		  if (!img.data)  {  
			std::cout << "File " << file << " not found\n";  
			exit(1);  
		  }  
		  
		  img = trainPrePos(img); 
		 imshow("after img = trainPrePos(img)", img);
		  img = img.reshape(1,1);
		 
		  imshow("after img.reshape(1,1)", img);
		  cout << img.size() << endl;
		  
		  for(int i=0;i<SX*SY;++i) {
		  		cout  << "img.at<float>(0,i =" << i  <<  " j " <<  j << "|" << img.at<float>(0,i) <<")"  <<  endl;
				features.at<float>(j,i)=float(img.at<uchar>(0,i));
			}
			labels.at<int>(0,j)=j;	
			cout << "labels" << labels.at<float>(0,j) << endl;
	 }  
  
} 

Mat trainPrePos(Mat img)   A
{   
	imshow("Image Before BGRA2GRAY", img);
	
	cvtColor(img,img,cv::COLOR_BGRA2GRAY);
	imshow("Image After BGRA2GRAY", img);
	
	GaussianBlur(img, img, Size(5, 5), 2, 2); 
	imshow("After GaussianBlur", img);
	
	adaptiveThreshold(img, img, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 11, 2);  
	imshow("After adaptiveThreshold", img);
	
       THRESH_BINARY     = 0,
       THRESH_BINARY_INV = 1,
       THRESH_TRUNC      = 2,
       THRESH_TOZERO     = 3,
       THRESH_TOZERO_INV = 4,
       THRESH_MASK       = 7,
       THRESH_OTSU       = 8,
       THRESH_TRIANGLE   = 16,
};
	
	
	
	
	 std::vector<std::vector<Point> >contours; 
	
	 Mat contourImage,out;  
	 
	 img.copyTo(contourImage);  
	
	 findContours(contourImage, contours, cv::RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);  
  
         cout << "Total contours" << contours.size() << endl;
	 int idx = 0;  
	 size_t area = 0;  
	 for (size_t i = 0; i < contours.size(); i++)  {  
	 cout << "i = "<< i << "controurse[i].size" << contours[i].size() << endl;
		  if (area < contours[i].size() )  {  
			   idx = i;  
			   area = contours[i].size();  
		  }  
	 }  
  
	 Rect rec = boundingRect(contours[idx]);  
  	
	 resize(img(rec),out, Size(SX, SY));  
	
	
	 
	 for (int  q = 0;  q < 5; q++)
	 cout << "q" << q << contours[q];
	 
	  waitKey(0);
	 
	 
	 return out;  
}
Does this even compile?
1
2
3
4
std::vector<std::vector<Point> >contours; 
...	 
for (int  q = 0;  q < 5; q++)
    cout << "q" << q << contours[q];

Unless you've overloaded the << operator for vector<Point>, this isn't going to work.

1. How to output all contours contains? If i set loop condition to 6 it gives segmentation error.
Well the first question would be, why 6 and not contours.size() ?

The second question is, if it's crashing, then compile with "-g" and run the program in the debugger.
You get a lot more information than "it doesn't work".



2. How can i output contours to text file without using constructor?
The same way you cout them.
Last edited on
Topic archived. No new replies allowed.