Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OpenCVApplication.cpp Defines the entry point for the console application.
- #include stdafx.h
- #include common.h
- #include math.h
- #include queue
- #include random
- void testOpenImage()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src;
- src = imread(fname);
- imshow(image, src);
- waitKey();
- }
- }
- void testOpenImagesFld()
- {
- char folderName[MAX_PATH];
- if (openFolderDlg(folderName) == 0)
- return;
- char fname[MAX_PATH];
- FileGetter fg(folderName, bmp);
- while (fg.getNextAbsFile(fname))
- {
- Mat src;
- src = imread(fname);
- imshow(fg.getFoundFileName(), src);
- if (waitKey() == 27) ESC pressed
- break;
- }
- }
- void testResize()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src;
- src = imread(fname);
- Mat dst1, dst2;
- without interpolation
- resizeImg(src, dst1, 320, false);
- with interpolation
- resizeImg(src, dst2, 320, true);
- imshow(input image, src);
- imshow(resized image (without interpolation), dst1);
- imshow(resized image (with interpolation), dst2);
- waitKey();
- }
- }
- void testVideoSequence()
- {
- VideoCapture cap(Videosrubic.avi); off-line video from file
- VideoCapture cap(0); live video from web cam
- if (!cap.isOpened()) {
- printf(Cannot open video capture device.n);
- waitKey(0);
- return;
- }
- Mat edges;
- Mat frame;
- char c;
- while (cap.read(frame))
- {
- Mat grayFrame;
- cvtColor(frame, grayFrame, CV_BGR2GRAY);
- imshow(source, frame);
- imshow(gray, grayFrame);
- c = cvWaitKey(0); waits a key press to advance to the next frame
- if (c == 27) {
- press ESC to exit
- printf(ESC pressed - capture finishedn);
- break; ESC pressed
- };
- }
- }
- void testSnap()
- {
- VideoCapture cap(0); open the deafult camera (i.e. the built in web cam)
- VideoCapture cap(Videosmegamind.avi);
- if (!cap.isOpened()) openenig the video device failed
- {
- printf(Cannot open video capture device.n);
- return;
- }
- Mat frame;
- char numberStr[256];
- char fileName[256];
- video resolution
- Size capS = Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),
- (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
- Display window
- const char WIN_SRC = Src; window for the source frame
- namedWindow(WIN_SRC, CV_WINDOW_AUTOSIZE);
- cvMoveWindow(WIN_SRC, 0, 0);
- const char WIN_DST = Snapped; window for showing the snapped frame
- namedWindow(WIN_DST, CV_WINDOW_AUTOSIZE);
- cvMoveWindow(WIN_DST, capS.width + 10, 0);
- char c;
- int frameNum = -1;
- int frameCount = 0;
- for (;;)
- {
- cap frame; get a new frame from camera
- if (frame.empty())
- {
- printf(End of the video filen);
- break;
- }
- ++frameNum;
- imshow(WIN_SRC, frame);
- c = cvWaitKey(10); waits a key press to advance to the next frame
- if (c == 27) {
- press ESC to exit
- printf(ESC pressed - capture finished);
- break; ESC pressed
- }
- if (c == 115){ 's' pressed - snapp the image to a file
- frameCount++;
- fileName[0] = NULL;
- sprintf(numberStr, %d, frameCount);
- strcat(fileName, ImagesA);
- strcat(fileName, numberStr);
- strcat(fileName, .bmp);
- bool bSuccess = imwrite(fileName, frame);
- if (!bSuccess)
- {
- printf(Error writing the snapped imagen);
- }
- else
- imshow(WIN_DST, frame);
- }
- }
- }
- void MyCallBackFunc(int event, int x, int y, int flags, void param)
- {
- More examples httpopencvexamples.blogspot.com201401detect-mouse-clicks-and-moves-on-image.html
- Mat src = (Mat)param;
- Vec3b buf;
- if (event == CV_EVENT_FLAG_LBUTTON)
- {
- printf(Pos(x,y) %d,%d Color(RGB) %d,%d,%dn,
- x, y,
- (int)(src).atVec3b(y, x)[2],
- (int)(src).atVec3b(y, x)[1],
- (int)(src).atVec3b(y, x)[0]);
- }
- if (event == CV_EVENT_LBUTTONDBLCLK){
- buf[2] = (src).atVec3b(y, x)[2];
- buf[1] = (src).atVec3b(y, x)[1];
- buf[0] = (src).atVec3b(y, x)[0];
- float r_max = -1, r_min = 10000, c_max = -1, c_min = 10000;
- int area = 0, V = 0, W = 0;
- float fi, P = 0, com_rows = 0, com_cols = 0, T;
- for (int i = 0; i src-rows; i++){
- for (int j = 0; j src-cols; j++){
- if (src-atVec3b(i, j) == buf)
- {
- area++;
- com_rows += i;
- com_cols += j;
- if (src-atVec3b(i + 1, j) != buf
- src-atVec3b(i + 1, j + 1) != buf
- src-atVec3b(i + 1, j - 1) != buf
- src-atVec3b(i, j - 1) != buf
- src-atVec3b(i, j + 1) != buf
- src-atVec3b(i - 1, j - 1) != buf
- src-atVec3b(i - 1, j) != buf
- src-atVec3b(i - 1, j + 1) != buf)
- P++;
- if (ir_max)r_max = i;
- if (ir_min)r_min = i;
- if (jc_max)c_max = j;
- if (jc_min)c_min = j;
- }
- }
- }
- com_rows = ((float)1 area)com_rows;
- com_cols = ((float)1 area)com_cols;
- for (int i = 0; i src-rows; i++){
- for (int j = 0; j src-cols; j++){
- if (src-atVec3b(i, j) == buf)
- {
- V += ((i - com_rows)(j - com_cols));
- W += ((j - com_cols)(j - com_cols) - (i - com_rows)(i - com_rows));
- }
- }
- }
- printf(Area is %dn, area);
- printf(Center of mass is (%f,%f)n, com_rows, com_cols);
- V = 2;
- fi = 0.5atan2(V, W);
- fi = fi 180 CV_PI;
- if (fi 0)fi = fi + 180;
- printf(Axis of elongation is %f degreesn, fi);
- P = (CV_PI 4)P;
- printf(Perimeter is %fn, P);
- T = (4 CV_PI)(area (PP));
- printf(Thinness ratio is %fn, T);
- printf(Aspect ratio is %fn, (c_max - c_min + 1) (r_max - r_min + 1));
- }
- trasam axa de alungire
- A(cmin,rbara+tg(fi)(cmin-cbara))
- B(cmax,rbara+rg(fi)(cmax-cbara))
- line(img,point(xa,ya),point(xb,yb),CV_RGB(130,70,240),2);
- Mat d=s.clone() face copie la imagine
- proiectie pe orizontala cu sir de frecventa lipim de stanga, verticala de pamant
- }
- void testMouseClick()
- {
- Mat src;
- Read image from file
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- src = imread(fname);
- Create a window
- namedWindow(My Window, 1);
- set the callback function for any mouse event
- setMouseCallback(My Window, MyCallBackFunc, &src);
- show the image
- imshow(My Window, src);
- Wait until user press some key
- waitKey(0);
- }
- }
- Histogram display function - display a histogram using bars (simlilar to L3 PI)
- Input
- name - destination (output) window name
- hist - pointer to the vector containing the histogram values
- hist_cols - no. of bins (elements) in the histogram = histogram image width
- hist_height - height of the histogram image
- Call example
- showHistogram (MyHist, hist_dir, 255, 200);
- void showHistogram(const string& name, int hist, const int hist_cols, const int hist_height)
- {
- Mat imgHist(hist_height, hist_cols, CV_8UC3, CV_RGB(255, 255, 255)); constructs a white image
- computes histogram maximum
- int max_hist = 0;
- for (int i = 0; ihist_cols; i++)
- if (hist[i] max_hist)
- max_hist = hist[i];
- double scale = 1.0;
- scale = (double)hist_height max_hist;
- int baseline = hist_height - 1;
- for (int x = 0; x hist_cols; x++) {
- Point p1 = Point(x, baseline);
- Point p2 = Point(x, baseline - cvRound(hist[x] scale));
- line(imgHist, p1, p2, CV_RGB(255, 0, 255)); histogram bins colored in magenta
- }
- imshow(name, imgHist);
- }
- void negative_image(){
- uchar pixel;
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- Mat img = imread(fname, -1);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- img.atuchar(i, j) = 255 - img.atuchar(i, j);
- }
- }
- imshow(negative image, img);
- waitKey(0);
- }
- }
- void additive_grey_change(){
- int val;
- char fname[10000];
- while (openFileDlg(fname))
- {
- printf(introdu valoarean);
- stdcin val;
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(My Window, img);
- Mat img = imread(fname, -1);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if ((int)img.atuchar(i, j) + (int)val 255)
- {
- img.atuchar(i, j) = 255;
- }
- else{
- img.atuchar(i, j) = img.atuchar(i, j) + val;
- }
- }
- }
- imshow(gray change, img);
- waitKey(0);
- }
- }
- void color_matrix(){
- float vals[256 256];
- Mat img(256, 256, CV_8UC3, vals);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (i 128 && j 128)
- img.atVec3b(i, j) = Vec3b(255, 255, 255);
- if (i 128 && j 128)
- img.atVec3b(i, j) = Vec3b(0, 0, 255);
- if (i 128 && j 128)
- img.atVec3b(i, j) = Vec3b(0, 255, 0);
- if (i 128 && j 128)
- img.atVec3b(i, j) = Vec3b(0, 255, 255);
- }
- }
- imshow(window, img);
- waitKey(0);
- }
- void compute_inverse(){
- float vals[9] = { 1, 2, 0, 4, 1, 0, 0, 0, 1 };
- Mat M(3, 3, CV_32F, vals);
- stdcout M stdendl;
- M = M.inv();
- stdcout M stdendl;
- stdgetchar();
- stdgetchar();
- }
- void split_channels(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_COLOR);
- imshow(original image, img);
- Mat imR(img.rows, img.cols, CV_8UC1), imG(img.rows, img.cols, CV_8UC1), imB(img.rows, img.cols, CV_8UC1);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- imR.atuchar(i, j) = img.atVec3b(i, j)[2];
- imG.atuchar(i, j) = img.atVec3b(i, j)[1];
- imB.atuchar(i, j) = img.atVec3b(i, j)[0];
- }
- }
- imshow(R, imR);
- imshow(G, imG);
- imshow(B, imB);
- waitKey(0);
- }
- }
- void color_to_grayscale(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_COLOR);
- imshow(original image, img);
- Mat imgr(img.rows, img.cols, CV_8UC1);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- imgr.atuchar(i, j) = (img.atVec3b(i, j)[2] + img.atVec3b(i, j)[1] + img.atVec3b(i, j)[0]) 3;
- }
- }
- imshow(grayscale, imgr);
- waitKey(0);
- }
- }
- void grayscale_to_bw(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- Mat imgr(img.rows, img.cols, CV_8UC1);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (img.atuchar(i, j) 160){
- imgr.atuchar(i, j) = 255;
- }
- else{
- imgr.atuchar(i, j) = 0;
- }
- }
- }
- imshow(black and white, imgr);
- waitKey(0);
- }
- }
- void rgb_to_hsv(){
- char fname[10000];
- float r, g, b, M, m, C, H, S, V;
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_COLOR);
- imshow(original image, img);
- Mat imH(img.rows, img.cols, CV_8UC1), imS(img.rows, img.cols, CV_8UC1), imV(img.rows, img.cols, CV_8UC1);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- r = (float)img.atVec3b(i, j)[2] 255;
- g = (float)img.atVec3b(i, j)[1] 255;
- b = (float)img.atVec3b(i, j)[0] 255;
- M = max(max(r, g), b);
- m = min(min(r, g), b);
- C = M - m;
- V = M;
- if (C)
- S = C V;
- else
- S = 0;
- if (C){
- if (M == r)H = 60 (g - b) C;
- if (M == g)H = 120 (b - r) C;
- if (M == b)H = 240 (r - g) C;
- }
- else{
- H = 0;
- }
- if (H 0)H = H + 360;
- imH.atuchar(i, j) = H 255 360;
- imS.atuchar(i, j) = S 255;
- imV.atuchar(i, j) = V 255;
- }
- }
- imshow(H, imH);
- imshow(S, imS);
- imshow(V, imV);
- waitKey(0);
- }
- }
- int histValues[256];
- float normalizedHistoValues[256];
- void histogram(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- histValues[img.atuchar(i, j)]++;
- }
- }
- showHistogram(MyHist, histValues, 256, 500);
- for (int i = 0; i 256; i++)
- {
- histValues[i] = 0;
- }
- }
- }
- int result[256];
- void normalizeHistogram(int histogram, int size){
- for (int i = 0; i 256; i++)
- normalizedHistoValues[i] = (float)histValues[i] size;
- }
- void ml_thresholding(){
- int WH = 5;
- float TH = 0.0003;
- float miu;
- int dimension = 1;
- boolean ok;
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- histValues[img.atuchar(i, j)]++;
- }
- }
- normalizeHistogram(histValues, img.rowsimg.cols);
- for (int k = 0 + WH; k 255 - WH; k++){
- miu = 0;
- ok = true;
- for (int i = k - WH; i k + WH; i++){
- miu += normalizedHistoValues[i];
- if (normalizedHistoValues[k] normalizedHistoValues[i])ok = false;
- }
- miu = miu (2 WH + 1);
- if (normalizedHistoValues[k] miu + TH &&ok){
- result[dimension++] = k;
- }
- }
- result[0] = 0;
- result[dimension] = 255;
- dimension++;
- for (int i = 0; i dimension; i++){
- stdcout result[i] n stdendl;
- }
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- int maxx = -1;
- for (int k = 0; k dimension - 1; k++){
- if (img.atuchar(i, j)result[k] && img.atuchar(i, j)result[k + 1]){
- if (img.atuchar(i, j) - result[k] result[k + 1] - img.atuchar(i, j))img.atuchar(i, j) = result[k];
- else img.atuchar(i, j) = result[k + 1];
- }
- }
- }
- }
- imshow(thresholded image, img);
- }
- }
- uchar limit_value(int value){
- if (value 0)return 0;
- else if (value 255)return 255;
- else return value;
- }
- void floyd_steinberg(){
- int WH = 5;
- float TH = 0.0003;
- float miu;
- int dimension = 1;
- boolean ok;
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- histValues[img.atuchar(i, j)]++;
- }
- }
- normalizeHistogram(histValues, img.rowsimg.cols);
- for (int k = 0 + WH; k 255 - WH; k++){
- miu = 0;
- ok = true;
- for (int i = k - WH; i k + WH; i++){
- miu += normalizedHistoValues[i];
- if (normalizedHistoValues[k] normalizedHistoValues[i])ok = false;
- }
- miu = miu (2 WH + 1);
- if (normalizedHistoValues[k] miu + TH &&ok){
- result[dimension++] = k;
- }
- }
- result[0] = 0;
- result[dimension] = 255;
- dimension++;
- for (int i = 0; i dimension; i++){
- stdcout result[i] n stdendl;
- }
- for (int i = 0; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- int oldValue = img.atuchar(i, j);
- int newValue;
- float error;
- for (int k = 0; k dimension - 1; k++){
- if (img.atuchar(i, j)result[k] && img.atuchar(i, j)result[k + 1]){
- if (img.atuchar(i, j) - result[k] result[k + 1] - img.atuchar(i, j))newValue = result[k];
- else newValue = result[k + 1];
- }
- }
- error = oldValue - newValue;
- img.atuchar(i, j) = newValue;
- img.atuchar(i, j + 1) = img.atuchar(i, j + 1) + limit_value((int)(7 error 16));
- img.atuchar(i + 1, j - 1) = img.atuchar(i + 1, j - 1) + limit_value((int)(3 error 16));
- img.atuchar(i + 1, j) = img.atuchar(i + 1, j) + limit_value((int)(5 error 16));
- img.atuchar(i + 1, j + 1) = img.atuchar(i + 1, j + 1) + limit_value((int)(error 16));
- }
- }
- imshow(floyd image, img);
- }
- }
- void geom_feat()
- {
- Mat src;
- Read image from file
- char fname[MAX_PATH] = CUsersStudentDownloadsMultiple Objectsgeometrical_features.bmp;
- src = imread(fname, CV_LOAD_IMAGE_COLOR);
- Create a window
- namedWindow(My Window, 1);
- set the callback function for any mouse event
- setMouseCallback(My Window, MyCallBackFunc, &src);
- show the image
- imshow(My Window, src);
- Wait until user press some key
- waitKey(0);
- }
- void BFS_traversal(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- int label = 0;
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- Mat labels(img.rows, img.cols, CV_32SC1);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- labels.atint(i, j) = 0;
- }
- }
- Mat colored(img.rows, img.cols, CV_8UC3);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (img.atuchar(i, j) == 0 && labels.atint(i, j) == 0){
- label++;
- stdqueuePoint2i Q;
- labels.atint(i, j) = label;
- Q.push({ i, j });
- while (!Q.empty()){
- Point2i p = Q.front();
- Q.pop();
- int dx[10] = { -1, 0, 1, -1, 1, -1, 0, 1 };
- int dy[10] = { -1, -1, -1, 0, 0, 1, 1, 1 };
- for (int k = 0; k 8; k++){
- if (img.atuchar(p.x + dx[k], p.y + dy[k]) == 0 && labels.atint(p.x + dx[k], p.y + dy[k]) == 0){
- labels.atint(p.x + dx[k], p.y + dy[k]) = label;
- Q.push({ p.x + dx[k], p.y + dy[k] });
- }
- }
- }
- }
- }
- }
- stddefault_random_engine gen;
- stduniform_int_distributionint d(0, 255);
- Vec3b colors[1000];
- for (int i = 1; i = label; i++)
- {
- colors[i][0] = d(gen);
- colors[i][1] = d(gen);
- colors[i][2] = d(gen);
- }
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (labels.atint(i, j) 0){
- colored.atVec3b(i, j) = colors[labels.atint(i, j)];
- }
- }
- }
- imshow(colored labeled img, colored);
- waitKey(0);
- }
- }
- void two_pass_traversal(){
- char fname[10000];
- int dx[10] = { -1, 0, 1, -1 };
- int dy[10] = { -1, -1, -1, 0, };
- stdvectorstdvectorint edges;
- while (openFileDlg(fname))
- {
- int newLabels[1000] = {};
- int label = 0;
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- Mat labels(img.rows, img.cols, CV_32SC1);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- labels.atint(i, j) = 0;
- }
- }
- Mat colored(img.rows, img.cols, CV_8UC3);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- stdvectorint L;
- if (img.atuchar(i, j) == 0 && labels.atint(i, j) == 0){
- for (int k = 0; k 4; k++){
- if (labels.atint(i + dx[k], j + dy[k]) 0){
- L.push_back(labels.atint(i + dx[k], j + dy[k]));
- }
- }
- if (L.size() == 0){
- label++;
- edges.resize(label + 1);
- labels.atint(i, j) = label;
- }
- else{
- int x = 10000;
- for (int k = 0; k L.size(); k++)
- {
- if (L[k] x)x = L[k];
- }
- labels.atint(i, j) = x;
- for (int k = 0; k L.size(); k++)
- {
- if (L[k] != x){
- edges[x].push_back(L[k]);
- edges[L[k]].push_back(x);
- }
- }
- }
- }
- }
- }
- int newlabel = 0;
- for (int i = 0; i label; i++){
- if (newLabels[i] == 0){
- newlabel++;
- stdqueueint Q;
- newLabels[i] = newlabel;
- Q.push(i);
- while (!Q.empty()){
- int x = Q.front();
- Q.pop();
- for (int j = 0; j edges[x].size(); j++){
- if (newLabels[edges[x][j]] == 0){
- newLabels[edges[x][j]] = newlabel;
- Q.push(edges[x][j]);
- }
- }
- }
- }
- }
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (labels.atint(i, j)0)
- labels.atint(i, j) = newLabels[labels.atint(i, j)];
- }
- stdcout stdendl;
- }
- stddefault_random_engine gen;
- stduniform_int_distributionint d(0, 255);
- Vec3b colors[1000];
- for (int i = 1; i = label; i++)
- {
- colors[i][0] = d(gen);
- colors[i][1] = d(gen);
- colors[i][2] = d(gen);
- }
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (labels.atint(i, j) 0){
- colored.atVec3b(i, j) = colors[labels.atint(i, j)];
- }
- }
- }
- imshow(colored labeled img, colored);
- waitKey(0);
- }
- }
- int ac[100000], dc[1000000];
- void border_tracing(){
- char fname[10000];
- int dx[10] = { 1, 1, 0, -1, -1, -1, 0, 1 };
- int dy[10] = { 0, -1, -1, -1, 0, 1, 1, 1 };
- Point pn, pn_1, p1, p0;
- int n = 0, dir = 7, k = 1;
- int ok = 0;
- ac[0] = 7;
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- Mat imgr(img.rows, img.cols, CV_8UC1);
- for (int i = 0; i imgr.rows; i++)
- {
- for (int j = 0; j imgr.cols; j++)
- {
- imgr.atuchar(i, j) = 0;
- }
- }
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (img.atuchar(i, j) 150){
- p0.x = j;
- p0.y = i;
- pn = p0;
- do{
- ok = 1;
- if (dir % 2 == 0){
- dir = (dir + 7) % 8;
- }
- else{
- dir = (dir + 6) % 8;
- }
- while (img.atuchar(pn.y + dy[dir], pn.x + dx[dir]) != 0){
- dir = (dir + 1) % 8;
- }
- ac[k] = dir;
- dc[k++] = (ac[k] - ac[k - 1] + 8) % 8;
- pn_1 = pn;
- pn.x = pn.x + dx[dir];
- pn.y = pn.y + dy[dir];
- imgr.atuchar(pn.y, pn.x) = 255;
- if (n == 0)p1 = pn;
- n++;
- } while (!((pn == p1) && (pn_1 == p0) && (n = 2)));
- }
- if (ok == 1)j = img.cols;
- }
- if (ok == 1)i = img.rows;
- }
- stdcout ac ;
- for (int i = 0; i k; i++){
- stdcout ac[i] ;
- }
- stdcout stdendl dc ;
- for (int i = 0; i k; i++){
- stdcout dc[i] ;
- }
- imshow(grayscale, imgr);
- waitKey(0);
- }
- }
- void dilation(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- Mat dilated(img.rows, img.cols, CV_8UC1, Scalar(255));
- Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
- int n;
- stdcout n is;
- stdcin n;
- img.copyTo(intermediary);
- for (int k = 0; k n; k++){
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (intermediary.atuchar(i, j) == 0){
- dilated.atuchar(i, j) = 0;
- dilated.atuchar(i + 1, j) = 0;
- dilated.atuchar(i - 1, j) = 0;
- dilated.atuchar(i, j + 1) = 0;
- dilated.atuchar(i, j - 1) = 0;
- }
- }
- }
- dilated.copyTo(intermediary);
- dilated(img.rows, img.cols, CV_8UC1, Scalar(255));
- }
- imshow(dilated, dilated);
- waitKey(0);
- }
- }
- void erosion(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- Mat eroded(img.rows, img.cols, CV_8UC1, Scalar(255));
- Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
- int n;
- stdcout n is;
- stdcin n;
- img.copyTo(intermediary);
- for (int k = 0; k n; k++){
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (intermediary.atuchar(i, j) == 0 && intermediary.atuchar(i + 1, j) == 0 && intermediary.atuchar(i - 1, j) == 0 && intermediary.atuchar(i, j + 1) == 0 && intermediary.atuchar(i, j - 1) == 0){
- eroded.atuchar(i, j) = 0;
- }
- }
- }
- eroded.copyTo(intermediary);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- eroded.atuchar(i, j) = 255;
- }
- }
- }
- imshow(eroded, intermediary);
- waitKey(0);
- }
- }
- void opening(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- Mat eroded(img.rows, img.cols, CV_8UC1, Scalar(255));
- Mat dilated(img.rows, img.cols, CV_8UC1, Scalar(255));
- Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
- img.copyTo(intermediary);
- for (int k = 0; k n; k++){
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (intermediary.atuchar(i, j) == 0 && intermediary.atuchar(i + 1, j) == 0 && intermediary.atuchar(i - 1, j) == 0 && intermediary.atuchar(i, j + 1) == 0 && intermediary.atuchar(i, j - 1) == 0){
- eroded.atuchar(i, j) = 0;
- }
- }
- }
- eroded.copyTo(intermediary);
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (intermediary.atuchar(i, j) == 0){
- dilated.atuchar(i, j) = 0;
- dilated.atuchar(i + 1, j) = 0;
- dilated.atuchar(i - 1, j) = 0;
- dilated.atuchar(i, j + 1) = 0;
- dilated.atuchar(i, j - 1) = 0;
- }
- }
- }
- }
- imshow(opening, dilated);
- waitKey(0);
- }
- }
- void closing(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- Mat eroded(img.rows, img.cols, CV_8UC1, Scalar(255));
- Mat dilated(img.rows, img.cols, CV_8UC1, Scalar(255));
- Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
- img.copyTo(intermediary);
- for (int k = 0; k n; k++){
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (intermediary.atuchar(i, j) == 0){
- dilated.atuchar(i, j) = 0;
- dilated.atuchar(i + 1, j) = 0;
- dilated.atuchar(i - 1, j) = 0;
- dilated.atuchar(i, j + 1) = 0;
- dilated.atuchar(i, j - 1) = 0;
- }
- }
- }
- dilated.copyTo(intermediary);
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (intermediary.atuchar(i, j) == 0 && intermediary.atuchar(i + 1, j) == 0 && intermediary.atuchar(i - 1, j) == 0 && intermediary.atuchar(i, j + 1) == 0 && intermediary.atuchar(i, j - 1) == 0){
- eroded.atuchar(i, j) = 0;
- }
- }
- }
- }
- imshow(closing, eroded);
- waitKey(0);
- }
- }
- void boundary(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- Mat boundary(img.rows, img.cols, CV_8UC1, Scalar(255));
- Mat eroded(img.rows, img.cols, CV_8UC1, Scalar(255));
- Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
- img.copyTo(intermediary);
- for (int k = 0; k n; k++){
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (intermediary.atuchar(i, j) == 0 && intermediary.atuchar(i + 1, j) == 0 && intermediary.atuchar(i - 1, j) == 0 && intermediary.atuchar(i, j + 1) == 0 && intermediary.atuchar(i, j - 1) == 0){
- eroded.atuchar(i, j) = 0;
- }
- }
- }
- dilated.copyTo(intermediary);
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (img.atuchar(i, j) == 0 && eroded.atuchar(i, j) != 0){
- boundary.atuchar(i, j) = 0;
- }
- }
- }
- }
- imshow(boundary, boundary);
- waitKey(0);
- }
- }
- void filling(){
- char fname[10000];
- Point2d p = Point2d(250, 100);
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- Mat expansion(img.rows, img.cols, CV_8UC1, Scalar(255));
- Mat final(img.rows, img.cols, CV_8UC1, Scalar(255));
- Mat intermediary(img.rows, img.cols, CV_8UC1, Scalar(255));
- intermediary.atuchar(100, 250) = 0;
- for (int k = 0; k n; k++){
- int ok;
- do
- {
- ok = 0;
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (intermediary.atuchar(i, j) == 0){
- if (img.atuchar(i, j) != 0){
- expansion.atuchar(i, j) = 0;
- }
- if (img.atuchar(i + 1, j) != 0){ expansion.atuchar(i + 1, j) = 0; }
- if (img.atuchar(i - 1, j) != 0){ expansion.atuchar(i - 1, j) = 0; }
- if (img.atuchar(i, j + 1) != 0){ expansion.atuchar(i, j + 1) = 0; }
- if (img.atuchar(i, j - 1) != 0){ expansion.atuchar(i, j - 1) = 0; }
- }
- }
- }
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (intermediary.atuchar(i, j) != expansion.atuchar(i, j))
- ok = 1;
- }
- }
- expansion.copyTo(intermediary);
- } while (ok);
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (img.atuchar(i, j) == 0 intermediary.atuchar(i, j) == 0){
- final.atuchar(i, j) = 0;
- }
- }
- }
- }
- imshow(fill, final);
- waitKey(0);
- }
- }
- float mean(char fname[]){
- float miu;
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- int sum = 0;
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- sum += img.atuchar(i, j);
- }
- }
- miu = (float)(sum (img.rowsimg.cols));
- return miu;
- }
- void mean_and_std_dev(){
- char fname[10000];
- float miu, st_dev;
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original image, img);
- int sum = 0;
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- sum += img.atuchar(i, j);
- }
- }
- miu = (float)(sum (img.rowsimg.cols));
- stdcout mean isn;
- stdcout miu stdendl;
- sum = 0;
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- sum += (img.atuchar(i, j) - miu)(img.atuchar(i, j) - miu);
- }
- }
- st_dev = sqrt(sum (img.rowsimg.cols));
- stdcout std dev isn;
- stdcout st_dev stdendl;
- waitKey(0);
- }
- }
- void basic_global_thresh(){
- char fname[10000];
- float miu, epsilon = 1, miu1 = 0, miu2 = 0;
- int imin = 256, imax = 0;
- int t0, t1;
- while (openFileDlg(fname))
- {
- int histValues[256] = { 0 };
- miu = mean(fname);
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- histValues[img.atuchar(i, j)]++;
- }
- }
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (img.atuchar(i, j) imin)imin = img.atuchar(i, j);
- if (img.atuchar(i, j) imax)imax = img.atuchar(i, j);
- }
- }
- t0 = (imin + imax) 2;
- t1 = t0 + 3;
- while (t1 - t01 t1 - t0 -1)
- {
- miu1 = 0;
- miu2 = 0;
- int n1 = 0, n2 = 0;
- for (int i = 0; i t0; i++)
- {
- miu1 += ihistValues[i];
- n1 += histValues[i];
- }
- for (int i = t0; i 255; i++)
- {
- miu2 += i histValues[i];
- n2 += histValues[i];
- }
- miu1 = miu1 n1;
- miu2 = miu2 n2;
- t0 = t1;
- t1 = (miu1 + miu2) 2;
- }
- stdcout t0;
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (img.atuchar(i, j) t0)
- img.atuchar(i, j) = 0;
- else
- img.atuchar(i, j) = 255;
- }
- }
- imshow(alb negru, img);
- }
- }
- void bright_change(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- int n;
- stdcout value isn;
- stdcin n;
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, img);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (img.atuchar(i, j) + n 255)img.atuchar(i, j) = 255;
- else {
- if (img.atuchar(i, j) + n 0)img.atuchar(i, j) = 0;
- else
- img.atuchar(i, j) += n;
- }
- }
- }
- imshow(modified, img);
- }
- }
- void contrast_change(){
- char fname[10000];
- int imin = 256, imax = 0;
- int a, b, c, d;
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, img);
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- if (img.atuchar(i, j) imin)imin = img.atuchar(i, j);
- if (img.atuchar(i, j) imax)imax = img.atuchar(i, j);
- }
- }
- a = imin;
- b = imax;
- c = 20;
- d = 245;
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- img.atuchar(i, j) = (c + (img.atuchar(i, j) - a)((float)(d - c) (b - a)));
- }
- }
- imshow(modified, img);
- }
- }
- void gamma_change(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, img);
- Mat copy = img.clone();
- for (int i = 0; i img.rows; i++)
- {
- for (int j = 0; j img.cols; j++)
- {
- img.atuchar(i, j) = 255 pow((float)img.atuchar(i, j) 255, 0.5);
- copy.atuchar(i, j) = 255 pow((float)copy.atuchar(i, j) 255, 2);
- }
- }
- imshow(reduced gamma, img);
- imshow(increased gamma, copy);
- }
- }
- void histo_eq(){
- Mat img;
- char fname[MAX_PATH];
- openFileDlg(fname);
- img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original img, img);
- int L = 255;
- int M = img.colsimg.rows;
- int histogramArray[256] = {};
- float PDG[256] = {};
- float CPDG[256] = {};
- for (int i = 0; i img.rows; i++) {
- for (int j = 0; j img.cols; j++) {
- histogramArray[img.atuchar(i, j)] = histogramArray[img.atuchar(i, j)] + 1;
- }
- }
- PDG[0] = (float)histogramArray[0] M;
- CPDG[0] = PDG[0];
- for (int i = 1; i = 255; i++) {
- PDG[i] = (float)histogramArray[i] M;
- CPDG[i] = PDG[i] + CPDG[i - 1];
- }
- for (int i = 0; i img.rows; i++) {
- for (int j = 0; j img.cols; j++) {
- img.atuchar(i, j) = 255 CPDG[img.atuchar(i, j)];
- }
- }
- imshow(result img, img);
- waitKey(0);
- }
- void mean_filter_3x3(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- int filter[3][3] = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, img);
- Mat copy = img.clone();
- for (int i = 0; i 3; i++)
- {
- for (int j = 0; j 3; j++)
- {
- stdcout filter.atuchar(i, j) ;
- }
- stdcout stdendl;
- }
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- int sum = 0;
- for (int k = -1; k = 1; k++){
- for (int l = -1; l = 1; l++){
- sum += img.atuchar(i + k, j + l)filter[1 + k][1 + l];
- }
- }
- copy.atuchar(i, j) = sum 9;
- }
- }
- imshow(blurred, copy);
- }
- }
- void mean_filter_5x5(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, img);
- Mat copy = img.clone();
- for (int i = 2; i img.rows - 2; i++)
- {
- for (int j = 2; j img.cols - 2; j++)
- {
- int sum = 0;
- for (int k = -2; k = 2; k++){
- for (int l = -2; l = 2; l++){
- sum += img.atuchar(i + k, j + l);
- }
- }
- copy.atuchar(i, j) = sum 25;
- }
- }
- imshow(blurred, copy);
- }
- }
- void gaussian_filter_3x3(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- int filter[3][3] = { { 1, 2, 1 }, { 2, 4, 2 }, { 1, 2, 1 } };
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, img);
- Mat copy = img.clone();
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- int sum = 0;
- for (int k = -1; k = 1; k++){
- for (int l = -1; l = 1; l++){
- sum += img.atuchar(i + k, j + l)filter[1 + k][1 + l];
- }
- }
- copy.atuchar(i, j) = sum 16;
- }
- }
- imshow(blurred, copy);
- }
- }
- void laplace_filter_3x3(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- int filter[3][3] = { { -1, -1, -1 }, { -1, 8, -1 }, { -1, -1, -1 } };
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, img);
- Mat copy = img.clone();
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- int sum = 0;
- for (int k = -1; k = 1; k++){
- for (int l = -1; l = 1; l++){
- sum += img.atuchar(i + k, j + l)filter[1 + k][1 + l];
- }
- }
- if (sum 0)sum = 0;
- if (sum 255)sum = 255;
- copy.atuchar(i, j) = sum;
- }
- }
- imshow(blurred, copy);
- }
- }
- void high_pass_filter_3x3(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- int filter[3][3] = { { -1, -1, -1 }, { -1, 9, -1 }, { -1, -1, -1 } };
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, img);
- Mat copy = img.clone();
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- int sum = 0;
- for (int k = -1; k = 1; k++){
- for (int l = -1; l = 1; l++){
- sum += img.atuchar(i + k, j + l)filter[1 + k][1 + l];
- }
- }
- if (sum 0)sum = 0;
- if (sum 255)sum = 255;
- copy.atuchar(i, j) = sum;
- }
- }
- imshow(blurred, copy);
- }
- }
- void centering_transform(Mat img) {
- for (int i = 0; i img.rows; i++) {
- for (int j = 0; j img.cols; j++) {
- img.atfloat(i, j) = ((i + j) & 1) -img.atfloat(i, j) img.atfloat(i, j);
- }
- }
- }
- void logMagFourier() {
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original img, src);
- Mat srcf;
- src.convertTo(srcf, CV_32FC1);
- centering_transform(srcf);
- Mat fourier;
- dft(srcf, fourier, DFT_COMPLEX_OUTPUT);
- Mat channels[] = { Matzeros(src.size(), CV_32F), Matzeros(src.size(), CV_32F) };
- split(fourier, channels);
- Mat mag, phi;
- magnitude(channels[0], channels[1], mag);
- phase(channels[0], channels[1], phi);
- Mat sho(mag.rows, mag.cols, CV_8UC1);
- float maxlog = 0;
- for (int i = 0; i mag.rows; i++) {
- for (int j = 0; j mag.cols; j++) {
- (log(mag.atfloat(i, j) + 1) maxlog maxlog = log(mag.atfloat(i, j) + 1) NULL);
- }
- }
- for (int i = 0; i mag.rows; i++) {
- for (int j = 0; j mag.cols; j++) {
- sho.atuchar(i, j) = 255 (log(mag.atfloat(i, j) + 1) maxlog);
- }
- }
- imshow(Result, sho);
- waitKey(0);
- }
- }
- void lowpass() {
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- Mat srcf;
- src.convertTo(srcf, CV_32FC1);
- imshow(original, src);
- centering_transform(srcf);
- Mat fourier;
- dft(srcf, fourier, DFT_COMPLEX_OUTPUT);
- Mat channels[] = { Matzeros(src.size(), CV_32F), Matzeros(src.size(), CV_32F) };
- split(fourier, channels);
- Mat mag, phi;
- magnitude(channels[0], channels[1], mag);
- phase(channels[0], channels[1], phi);
- float R = 10;
- for (int i = 0; i mag.rows; i++) {
- for (int j = 0; j mag.cols; j++) {
- channels[0].atfloat(i, j) = ((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j)) = RR channels[0].atfloat(i, j) 0);
- channels[1].atfloat(i, j) = ((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j)) = RR channels[1].atfloat(i, j) 0);
- }
- }
- Mat dstf, dst;
- merge(channels, 2, fourier);
- dft(fourier, dstf, DFT_INVERSE DFT_REAL_OUTPUT DFT_SCALE);
- centering_transform(dstf);
- normalize(dstf, dst, 0, 255, NORM_MINMAX, CV_8UC1);
- imshow(Result, dst);
- waitKey(0);
- }
- }
- void highpass() {
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, src);
- Mat srcf;
- src.convertTo(srcf, CV_32FC1);
- centering_transform(srcf);
- Mat fourier;
- dft(srcf, fourier, DFT_COMPLEX_OUTPUT);
- Mat channels[] = { Matzeros(src.size(), CV_32F), Matzeros(src.size(), CV_32F) };
- split(fourier, channels);
- Mat mag, phi;
- magnitude(channels[0], channels[1], mag);
- phase(channels[0], channels[1], phi);
- float R = 10;
- for (int i = 0; i mag.rows; i++) {
- for (int j = 0; j mag.cols; j++) {
- channels[0].atfloat(i, j) = ((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j)) RR channels[0].atfloat(i, j) 0);
- channels[1].atfloat(i, j) = ((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j)) RR channels[1].atfloat(i, j) 0);
- }
- }
- Mat dstf, dst;
- merge(channels, 2, fourier);
- dft(fourier, dstf, DFT_INVERSE DFT_REAL_OUTPUT DFT_SCALE);
- centering_transform(dstf);
- normalize(dstf, dst, 0, 255, NORM_MINMAX, CV_8UC1);
- imshow(Rez, dst);
- waitKey(0);
- }
- }
- void gaussianLowpass() {
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, src);
- Mat srcf;
- src.convertTo(srcf, CV_32FC1);
- centering_transform(srcf);
- Mat fourier;
- dft(srcf, fourier, DFT_COMPLEX_OUTPUT);
- Mat channels[] = { Matzeros(src.size(), CV_32F), Matzeros(src.size(), CV_32F) };
- split(fourier, channels);
- Mat mag, phi;
- magnitude(channels[0], channels[1], mag);
- phase(channels[0], channels[1], phi);
- float R = 10, A = 10;
- for (int i = 0; i mag.rows; i++) {
- for (int j = 0; j mag.cols; j++) {
- channels[0].atfloat(i, j) = channels[0].atfloat(i, j) (pow(2.7, -(((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j))) (AA))));
- channels[1].atfloat(i, j) = channels[1].atfloat(i, j) (pow(2.7, -(((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j))) (AA))));
- }
- }
- Mat dstf, dst;
- merge(channels, 2, fourier);
- dft(fourier, dstf, DFT_INVERSE DFT_REAL_OUTPUT DFT_SCALE);
- centering_transform(dstf);
- normalize(dstf, dst, 0, 255, NORM_MINMAX, CV_8UC1);
- imshow(Result, dst);
- waitKey(0);
- }
- }
- void gaussianHighpass() {
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, src);
- Mat srcf;
- src.convertTo(srcf, CV_32FC1);
- centering_transform(srcf);
- Mat fourier;
- dft(srcf, fourier, DFT_COMPLEX_OUTPUT);
- Mat channels[] = { Matzeros(src.size(), CV_32F), Matzeros(src.size(), CV_32F) };
- split(fourier, channels);
- Mat mag, phi;
- magnitude(channels[0], channels[1], mag);
- phase(channels[0], channels[1], phi);
- float R = 10, A = 5;
- for (int i = 0; i mag.rows; i++) {
- for (int j = 0; j mag.cols; j++) {
- channels[0].atfloat(i, j) = channels[0].atfloat(i, j) (1 - (pow(2.7, -(((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j))) (AA)))));
- channels[1].atfloat(i, j) = channels[1].atfloat(i, j) (1 - (pow(2.7, -(((((mag.rows 2) - i) ((mag.rows 2) - i)) + (((mag.cols 2) - j) ((mag.cols 2) - j))) (AA)))));
- }
- }
- Mat dstf, dst;
- merge(channels, 2, fourier);
- dft(fourier, dstf, DFT_INVERSE DFT_REAL_OUTPUT DFT_SCALE);
- centering_transform(dstf);
- normalize(dstf, dst, 0, 255, NORM_MINMAX, CV_8UC1);
- imshow(Result, dst);
- waitKey(0);
- }
- }
- void median_filter(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, img);
- Mat copy = img.clone();
- vectoruchar v;
- int w;
- stdcin w;
- for (int i = w 2; i img.rows - w 2; i++)
- {
- for (int j = w 2; j img.cols - w 2; j++)
- {
- for (int k = -w 2; k = w 2; k++){
- for (int l = -w 2; l = w 2; l++){
- v.push_back(img.atuchar(i + k, j + l));
- }
- }
- sort(v.begin(), v.end());
- copy.atuchar(i, j) = v[v.size() 2 + 1];
- v.clear();
- }
- }
- imshow(result, copy);
- }
- }
- void gaussian_filter_1x2d(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- float filter[50][50];
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, img);
- Mat copy = img.clone();
- vectoruchar v;
- int w;
- stdcin w;
- int x0 = w 2;
- int y0 = w 2;
- float sigma = w 6.0f;
- float sumaelementelordinnucleuldeconvolutie = 0;
- double t = (double)getTickCount();
- for (int i = 0; i w; i++)
- {
- for (int j = 0; j w; j++)
- {
- filter[i][j] = (float)(1.0f 2 CV_PIsigmasigma)(float)exp(((i - x0)(i - x0) + (j - y0)(j - y0)) -(2 sigmasigma));
- sumaelementelordinnucleuldeconvolutie += filter[i][j];
- stdcout filter[i][j] ;
- }
- stdcout stdendl;
- }
- for (int i = w 2; i img.rows - w 2; i++)
- {
- for (int j = w 2; j img.cols - w 2; j++)
- {
- float sum = 0;
- for (int k = -w 2; k = w 2; k++){
- for (int l = -w 2; l = w 2; l++){
- sum += img.atuchar(i + k, j + l)filter[w 2 + k][w 2 + l];
- }
- }
- copy.atuchar(i, j) = (int)(sum sumaelementelordinnucleuldeconvolutie);
- }
- }
- imshow(result, copy);
- t = (((double)getTickCount() - t) getTickFrequency());
- printf(time=%.3f [ms]n, t 1000);
- }
- }
- void gaussian_filter_2x1d(){
- char fname[10000];
- while (openFileDlg(fname))
- {
- float filter[50][50];
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow(original, img);
- Mat copy = img.clone();
- vectoruchar v;
- int w;
- stdcin w;
- int x0 = w 2;
- int y0 = w 2;
- float sigma = w 6.0f;
- float sumaelementelordinnucleuldeconvolutie1 = 0;
- float sumaelementelordinnucleuldeconvolutie2 = 0;
- vectorfloat Gx, Gy;
- double t = (double)getTickCount();
- for (int i = 0; i w; i++)
- {
- for (int j = 0; j w; j++)
- {
- filter[i][j] = (float)(1.0f 2 CV_PIsigmasigma)(float)exp(((i - x0)(i - x0) + (j - y0)(j - y0)) -(2 sigmasigma));
- if (i == x0){
- Gx.push_back(filter[i][j]);
- sumaelementelordinnucleuldeconvolutie1 += filter[i][j];
- }
- if (j == y0){
- Gy.push_back(filter[i][j]);
- sumaelementelordinnucleuldeconvolutie2 += filter[i][j];
- }
- stdcout filter[i][j] ;
- }
- stdcout stdendl;
- }
- for (int i = w 2; i img.rows - w 2; i++)
- {
- for (int j = w 2; j img.cols - w 2; j++)
- {
- float sum = 0;
- for (int k = -w 2; k = w 2; k++){
- sum += img.atuchar(i + k, j)Gx[w 2 + k];
- }
- copy.atuchar(i, j) = (int)(sum sumaelementelordinnucleuldeconvolutie1);
- }
- }
- img = copy.clone();
- for (int i = w 2; i img.rows - w 2; i++)
- {
- for (int j = w 2; j img.cols - w 2; j++)
- {
- float sum = 0;
- for (int k = -w 2; k = w 2; k++){
- sum += img.atuchar(i, j + k)Gy[w 2 + k];
- }
- copy.atuchar(i, j) = (int)(sum sumaelementelordinnucleuldeconvolutie2);
- }
- }
- imshow(result, copy);
- t = (((double)getTickCount() - t) getTickFrequency());
- printf(time=%.3f [ms]n, t 1000);
- }
- }
- void canny(){
- char fname[10000];
- Mat m;
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- int Sx[3][3] = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
- int Sy[3][3] = { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };
- imshow(original, img);
- Mat copy = img.clone();
- Mat copy2 = img.clone();
- vectoruchar v;
- int w = 3;
- stdcin w;
- for (int i = w 2; i img.rows - w 2; i++)
- {
- for (int j = w 2; j img.cols - w 2; j++)
- {
- int Gx = 0;
- int Gy = 0;
- for (int k = -w 2; k = w 2; k++){
- for (int l = -w 2; l = w 2; l++){
- Gx += img.atuchar(i + k, j + l)Sx[k + 1][l + 1];
- Gy += img.atuchar(i + k, j + l)Sy[k + 1][l + 1];
- }
- }
- float G = sqrt(GxGx + GyGy) (4 sqrt(2));
- float fi = atan2(Gy, Gx);
- float pi = CV_PI;
- copy.atuchar(i, j) = (uchar)G;
- v.clear();
- }
- }
- copy2 = copy.clone();
- for (int i = w 2; i img.rows - w 2; i++)
- {
- for (int j = w 2; j img.cols - w 2; j++)
- {
- int Gx = 0;
- int Gy = 0;
- for (int k = -w 2; k = w 2; k++){
- for (int l = -w 2; l = w 2; l++){
- Gx += img.atuchar(i + k, j + l)Sx[k + 1][l + 1];
- Gy += img.atuchar(i + k, j + l)Sy[k + 1][l + 1];
- }
- }
- float G = sqrt(GxGx + GyGy) (4 sqrt(2));
- float fi = atan2(Gy, Gx);
- float pi = CV_PI;
- 0
- if ((fipi 8 && fi-pi 8) (fi pi && fi7 pi 8) (fi -pi && fi-7 pi 8)){
- if (copy.atuchar(i, j) = copy.atuchar(i, j + 1) && copy.atuchar(i, j) = copy.atuchar(i, j - 1))copy2.atuchar(i, j) = copy.atuchar(i, j);
- else copy2.atuchar(i, j) = 0;
- }
- 1
- if ((fi pi 8 && fi3 pi 8) (fi-7 pi 8 && fi-5 pi 8)){
- if (copy.atuchar(i, j) = copy.atuchar(i - 1, j + 1) && copy.atuchar(i, j) = copy.atuchar(i + 1, j - 1))copy2.atuchar(i, j) = copy.atuchar(i, j);
- else copy2.atuchar(i, j) = 0;
- }
- 2
- if ((fi 3 pi 8 && fi 5 pi 8) (fi-3 pi 8 && fi-5 pi 8)){
- if (copy.atuchar(i, j) = copy.atuchar(i - 1, j) && copy.atuchar(i, j) = copy.atuchar(i + 1, j))copy2.atuchar(i, j) = copy.atuchar(i, j);
- else copy2.atuchar(i, j) = 0;
- }
- 3
- if ((fi 5 pi 8 && fi 7 pi 8) (fi-pi 8 && fi-3 pi 8)){
- if (copy.atuchar(i, j) = copy.atuchar(i + 1, j + 1) && copy.atuchar(i, j) = copy.atuchar(i - 1, j - 1))copy2.atuchar(i, j) = copy.atuchar(i, j);
- else copy2.atuchar(i, j) = 0;
- }
- }
- }
- imshow(partial result, copy);
- imshow(result, copy2);
- }
- }
- void adaptive_thresh(){
- Mat img=canny();
- int histValues[256] = { 0 };
- int nonZGradient = 0;
- for (int i = 1; i img.rows-1; i++)
- {
- for (int j = 1; j img.cols-1; j++)
- {
- histValues[img.atuchar(i, j)]++;
- if (img.atuchar(i, j) != 0)nonZGradient++;
- }
- }
- float p = 0.1;
- int noEdgePixels = pnonZGradient;
- int Th=0;
- stdcout noEdgePixels stdendl;;
- for (int i = 255; i = 0; i--)
- {
- if (histValues[i] != 0){
- noEdgePixels-=histValues[i];
- }
- if (noEdgePixels = 0){
- Th = i;
- i = 0;
- }
- }
- int Tl = 0.4Th;
- Th = 30;
- Tl = 12;
- stdcout Th Tl;
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (img.atuchar(i, j) = Tl){ img.atuchar(i, j) = 0; }
- else{
- if (img.atuchar(i, j) = Tl && img.atuchar(i, j) = Th){ img.atuchar(i, j) = 127; }
- else{
- if (img.atuchar(i, j) = Th){ img.atuchar(i, j) = 255; }
- }
- }
- }
- }
- imshow(thr, img);
- Mat final = img.clone();
- int stop = 0;
- while (!stop){
- stop = 1;
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (img.atuchar(i, j) == 255){
- for (int k = -1; k =1; k++){
- for (int l = -1; l = 1; l++){
- if (img.atuchar(i + k, j + l) == 127){
- img.atuchar(i + k, j + l) = 255;
- stop = 0;
- }
- }
- }
- }
- }
- }
- }
- for (int i = 1; i img.rows - 1; i++)
- {
- for (int j = 1; j img.cols - 1; j++)
- {
- if (img.atuchar(i, j) == 127)img.atuchar(i, j) = 0;
- }
- }
- imshow(final, img);
- waitKey(0);
- }
- int main()
- {
- int op;
- do
- {
- system(cls);
- destroyAllWindows();
- printf(Menun);
- printf(L1- 1 - Open imagen);
- printf(L1- 2 - Open BMP images from foldern);
- printf(L1- 3 - Resize imagen);
- printf(L1- 4 - Process videon);
- printf(L1- 5 - Snap frame from live videon);
- printf(L1- 6 - Mouse callback demon);
- printf(L1- 7 - Negative imagen);
- printf(L1- 8 - Additive greyn);
- printf(L1- 9 - Color matrixn);
- printf(L1- 10 - Compute inversen);
- printf(L2 - 11 - RGB24-split_channelsn);
- printf(L2 - 12 - Color to grayscalen);
- printf(L2 - 13 - grayscale to black&whiten);
- printf(L2 - 14 - RGB to HSVn);
- printf(L3 - 15 - Histogramn);
- printf(L3 - 16 - Multi level thresholdingn);
- printf(L3 - 17 - Floyd-Steinberg algorithmn);
- printf(L4 - 18 - Geometrical featuresn);
- printf(L5 - 19 - Breadth first traversaln);
- printf(L5 - 20 - Two pass traversaln);
- printf(L6 - 21 - Border tracing algorithmn);
- printf(L7 - 22 - Dilationn);
- printf(L7 - 23 - Erosionn);
- printf(L7 - 24 - Openingn);
- printf(L7 - 25 - Closingn);
- printf(L7 - 26 - Boundary extrn);
- printf(L7 - 27 - Region fillingn);
- printf(L8 - 28 - Mean and std devn);
- printf(L8 - 29 - Basic global threshold algon);
- printf(L8 - 30 - Brightness changen);
- printf(L8 - 31 - Contrast changen);
- printf(L8 - 32 - Gamma correctionn);
- printf(L8 - 33 - Histogram equalisationn);
- printf(L9 - 34 - Mean filter(3x3)n);
- printf(L9 - 35 - Mean filter(5x5)n);
- printf(L9 - 36 - Gaussian filter(3x3)n);
- printf(L9 - 37 - Laplace filter(3x3)n);
- printf(L9 - 38 - High pass filter(3x3)n);
- printf(L9 - 39 - Log magn Fouriern);
- printf(L9 - 40 - Ideal low pass filtern);
- printf(L9 - 41 - Ideal high pass filtern);
- printf(L9 - 42 - Gaussian cut LPFn);
- printf(L9 - 43 - Gaussian cut HPFn);
- printf(L10 - 44 - Median filtern);
- printf(L10 - 45 - Gaussian filter 1x2Dn);
- printf(L10 - 46 - Gaussian filter 2x1Dn);
- printf(L11 - 47 - Canny edge gaussian filteringn);
- printf(L12 - 48 - Adaptive thresholdingn);
- printf( 0 - Exitnn);
- printf(Option );
- scanf(%d, &op);
- switch (op)
- {
- case 1
- testOpenImage();
- break;
- case 2
- testOpenImagesFld();
- break;
- case 3
- testResize();
- break;
- case 4
- testVideoSequence();
- break;
- case 5
- testSnap();
- break;
- case 6
- testMouseClick();
- break;
- case 7
- negative_image();
- break;
- case 8
- additive_grey_change();
- break;
- case 9
- color_matrix();
- break;
- case 10
- compute_inverse();
- break;
- case 11
- split_channels();
- break;
- case 12
- color_to_grayscale();
- break;
- case 13
- grayscale_to_bw();
- break;
- case 14
- rgb_to_hsv();
- break;
- case 15
- histogram();
- break;
- case 16
- ml_thresholding();
- break;
- case 17
- floyd_steinberg();
- break;
- case 18
- geom_feat();
- break;
- case 19
- BFS_traversal();
- break;
- case 20
- two_pass_traversal();
- break;
- case 21
- border_tracing();
- break;
- case 22
- dilation();
- break;
- case 23
- erosion();
- break;
- case 24
- opening();
- break;
- case 25
- closing();
- break;
- case 26
- boundary();
- break;
- case 27
- filling();
- break;
- case 28
- mean_and_std_dev();
- break;
- case 29
- basic_global_thresh();
- break;
- case 30
- bright_change();
- break;
- case 31
- contrast_change();
- break;
- case 32
- gamma_change();
- break;
- case 33
- histo_eq();
- break;
- case 34
- mean_filter_3x3();
- break;
- case 35
- mean_filter_5x5();
- break;
- case 36
- gaussian_filter_3x3();
- break;
- case 37
- laplace_filter_3x3();
- break;
- case 38
- high_pass_filter_3x3();
- break;
- case 39
- logMagFourier();
- break;
- case 40
- lowpass();
- break;
- case 41
- highpass();
- break;
- case 42
- gaussianLowpass();
- break;
- case 43
- gaussianHighpass();
- break;
- case 44
- median_filter();
- break;
- case 45
- gaussian_filter_1x2d();
- break;
- case 46
- gaussian_filter_2x1d();
- break;
- case 47
- canny();
- break;
- case 48
- adaptive_thresh();
- break;
- }
- } while (op != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement