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
|
class OpenCV_Func {
public:
static void AutocropCany(cv::Mat& image, int hold=50, int lowThreshold =50, int ratio = 5, int kernel_size = 5);
struct PointValue {
long r, g, b;
};
};
void OpenCV_Func::AutocropCany(cv::Mat& image, int hold, int lowThreshold, int ratio, int kernel_size) {
cv::Mat image_gray, detected_edges;
// Convert the image to grayscale
cvtColor( image, image_gray, CV_BGR2GRAY );
// Reduce noise with a kernel 3x3
blur( image_gray, detected_edges, Size(kernel_size,kernel_size) );
// Canny detector
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
// Column and Row summary
int row_val[detected_edges.rows+1], col_val[detected_edges.cols+1];
memset(row_val, 0, sizeof(row_val));
memset(col_val, 0, sizeof(col_val));
int variant = 5;
uint8_t* pixelPtr = (uint8_t*) detected_edges.data;
int row_max = 0, col_max = 0;
int crossSize = 5;
for(int row = 0; row<detected_edges.rows; row++) {
int colCounter = 0;
for (int col =0; col<detected_edges.cols; col++) {
if (pixelPtr[row * detected_edges.cols + col] == 0) {
colCounter = 0;
} else {
if (++colCounter >= crossSize) row_val[row] = row_val[row]+1;
}
}
row_max = max(row_max, row_val[row]);
}
for(int i=variant; i<detected_edges.rows-variant; i++) {
int offset = detected_edges.cols * i;
for(int j=variant; j<detected_edges.cols-variant; j++) {
for (int count = -variant; count <= variant; count++) {
col_val[i] *= pixelPtr[detected_edges.cols * (i+variant) + j]>0;
}
col_val[i] /=5;
}
}
// Column and Row maximum
for (int i=0; i<detected_edges.cols-1; i++) col_max = max(col_max, col_val[i]);
int x_min = detected_edges.cols, x_max = 0;
int y_min = detected_edges.rows, y_max = 0;
// Get X_Min
int i=0;
while (col_val[i] < col_max/5 && i < detected_edges.cols) i++;
x_min = max(i-hold, 0);
// Get X_Max
i=detected_edges.cols;
while (col_val[i] < col_max/5 && i > 0) i--;
x_max = min(i+hold, detected_edges.cols-1);
i=46;
qDebug() << "BeforeQ" << i << row_val[i] << row_val[46] << sizeof(row_val) / sizeof(row_val[0]) << row_max << detected_edges.rows;
std::cout << "BeforeC " << i << " " << row_val[i] << " " << row_val[46] << " " << sizeof(row_val) / sizeof(row_val[0]) << " " << row_max << " " << detected_edges.rows << std::endl;
// Get Y_Min
i=0;
while (row_val[i] < 60) {
i++;
if (i==46) {
qDebug() << "DuringQ" << i << row_val[i] << row_val[46];
std::cout<< "DuringC " << i << " " << row_val[i] << " " << row_val[46] << std::endl;
}
}
...
}
|