Skip to content

Instantly share code, notes, and snippets.

@ArdWar
Created October 5, 2015 07:15
Show Gist options
  • Save ArdWar/32f06f64d00089686aa7 to your computer and use it in GitHub Desktop.
Save ArdWar/32f06f64d00089686aa7 to your computer and use it in GitHub Desktop.
[OpenCV] Adaptive threshold
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <stdio.h>
//void avrg(IplImage* buf2, int tresh[16]) {
// tresh = { };
// for (int a = 0;a < 4;a++) {
// for (int b = 0;b < 4;b++) {
// for (int x = 0;x < buf2->width / 4;x++) {
// for (int y = 0;y < buf2->height / 4;y++) {
// tresh[a*4+b] += ((uchar)buf2->imageData[buf2->widthStep*y + buf2->widthStep*buf2->height / 4 * b + x + buf2->width / 4 * a]) / (buf2->height* buf2->width / 16);
// }
// }
// }
// }
//}
//
//void proses1(IplImage* buf1, IplImage* out) {
// for (int x = 0;x < buf1->width;x++) {
// for (int y = 0;y < buf1->height;y++) {
// out->imageData[out->widthStep*y + x] = ((uchar)buf1->imageData[buf1->widthStep*y + x*3] +
// (uchar)buf1->imageData[buf1->widthStep*y + x*3+1] +
// (uchar)buf1->imageData[buf1->widthStep*y + x*3+2])/3;
// }
// }
//}
//
//void proses2(IplImage* buf2, IplImage* out, int thresh[16]) {
// for (int a = 0;a < 4;a++) {
// for (int b = 0;b < 4;b++) {
// for (int x = 0;x < buf2->width/4;x++) {
// for (int y = 0;y < buf2->height/4;y++) {
// if ((uchar)buf2->imageData[buf2->widthStep*y+buf2->widthStep*buf2->height/4*b + x + buf2->width/4*a] < thresh[a*4+b]) {
// out->imageData[out->widthStep*y + out->widthStep*buf2->height / 4 * b + x + buf2->width / 4 * a] = 0;
// }
// else {
// out->imageData[out->widthStep*y + out->widthStep*buf2->height / 4 * b + x + buf2->width / 4 * a] = 255;
// }
// }
// }
// }
// }
//}
void main() {
IplImage* img;
CvCapture* capture = cvCaptureFromCAM(0);
cvNamedWindow("Source",1);
cvNamedWindow("Greysc",1);
cvNamedWindow("Trshld",1);
for (;;) {
img = cvQueryFrame(capture);
int avrrg[16] = { };
IplImage* gray1 = cvCreateImage(cvSize(img->width, img->height), 8, 1);
IplImage* gray2 = cvCreateImage(cvSize(img->width, img->height), 8, 1);
// proses1(img, gray1);
// avrg(gray1, avrrg);
// avrrg = cvMean(gray1);
// proses2(gray1, gray2, avrrg);
cvCvtColor(img, gray1, CV_RGB2GRAY);
cvAdaptiveThreshold(gray1, gray2, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 9, 5);
cvShowImage("Source", img);
cvShowImage("Greysc", gray1);
cvShowImage("Trshld", gray2);
if (cvWaitKey(5) > 0)break;
cvReleaseImage(&gray1);
cvReleaseImage(&gray2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment