close
How to make auto-adjustments(brightness and contrast) for image Android Opencv Image Correction
1: /**
2: * \brief Automatic brightness and contrast optimization with optional histogram clipping
3: * \param [in]src Input image GRAY or BGR or BGRA
4: * \param [out]dst Destination image
5: * \param clipHistPercent cut wings of histogram at given percent tipical=>1, 0=>Disabled
6: * \note In case of BGRA image, we won't touch the transparency
7: */
8: void BrightnessAndContrastAuto(const cv::Mat &src, cv::Mat &dst, float clipHistPercent=0)
9: {
10:
11: CV_Assert(clipHistPercent >= 0);
12: CV_Assert((src.type() == CV_8UC1) || (src.type() == CV_8UC3) || (src.type() == CV_8UC4));
13:
14: int histSize = 256;
15: float alpha, beta;
16: double minGray = 0, maxGray = 0;
17:
18: //to calculate grayscale histogram
19: cv::Mat gray;
20: if (src.type() == CV_8UC1) gray = src;
21: else if (src.type() == CV_8UC3) cvtColor(src, gray, CV_BGR2GRAY);
22: else if (src.type() == CV_8UC4) cvtColor(src, gray, CV_BGRA2GRAY);
23: if (clipHistPercent == 0)
24: {
25: // keep full available range
26: cv::minMaxLoc(gray, &minGray, &maxGray);
27: }
28: else
29: {
30: cv::Mat hist; //the grayscale histogram
31:
32: float range[] = { 0, 256 };
33: const float* histRange = { range };
34: bool uniform = true;
35: bool accumulate = false;
36: calcHist(&gray, 1, 0, cv::Mat (), hist, 1, &histSize, &histRange, uniform, accumulate);
37:
38: // calculate cumulative distribution from the histogram
39: std::vector<float> accumulator(histSize);
40: accumulator[0] = hist.at<float>(0);
41: for (int i = 1; i < histSize; i++)
42: {
43: accumulator[i] = accumulator[i - 1] + hist.at<float>(i);
44: }
45:
46: // locate points that cuts at required value
47: float max = accumulator.back();
48: clipHistPercent *= (max / 100.0); //make percent as absolute
49: clipHistPercent /= 2.0; // left and right wings
50: // locate left cut
51: minGray = 0;
52: while (accumulator[minGray] < clipHistPercent)
53: minGray++;
54:
55: // locate right cut
56: maxGray = histSize - 1;
57: while (accumulator[maxGray] >= (max - clipHistPercent))
58: maxGray--;
59: }
60:
61: // current range
62: float inputRange = maxGray ...
A Filter Formulation for Computing Real Time Optical Flow
Visual odometry is obtained by the algorithm of Badino and Kanade [1] that minimizes the reprojection error of tracked features.
The source code is available on SourceForge.net: http://sourceforge.net/projects/qcv/
Visual Odometry (source code available)
Dense 3D reconstruction of a Street Segment
Farneback 光流算法详解与 calcOpticalFlowFarneback 源码分析
1: void calcOpticalFlowFarneback( const Mat& prev0, const Mat& next0, Mat& flow0,
2: double pyr_scale, int levels,
3: int winsize, int iterations, int poly_n, double poly_sigma, int flags )
4: {
5: ……
6: for( k = 0, scale = 1; k < levels; k++ )
7: {
8: scale *= pyr_scale;
9: if( prev0.cols*scale < min_size || prev0.rows*scale < min_size )
10: break;
11: }
12:
13: levels = k;
14:
15: for( k = levels; k >= 0; k-- )
16: {
17: for( i = 0, scale = 1; i < k; i++ )
18: scale *= pyr_scale;
19: ……
20: Mat R[2], I, M;
21: for( i = 0; i < 2; i++ )
22: {
23: img[i]->convertTo(fimg, CV_32F);
24: GaussianBlur(fimg, fimg, Size(smooth_sz, smooth_sz), sigma, sigma);
25: resize( fimg, I, Size(width, height), CV_INTER_LINEAR );
26: FarnebackPolyExp( I, R[i], poly_n, poly_sigma );
27: }
28:
29: FarnebackUpdateMatrices( R[0], R[1], flow, M, 0, flow.rows );
30:
31: for( i = 0; i < iterations; i++ )
32: {
33: if( flags & OPTFLOW_FARNEBACK_GAUSSIAN )
34: FarnebackUpdateFlow_GaussianBlur( R[0], R[1], flow, M, winsize, i < iterations - 1 );
35: else
36: FarnebackUpdateFlow_Blur( R[0], R[1], flow, M, winsize, i < iterations - 1 );
37: }
38:
39: prevFlow = flow;
40: }
41: }
Optical flow representation.
The color corresponds with the direction of the optical flow vector while the magnitude is encoded as the color intensity.
全站熱搜
留言列表