Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.28 KB | None | 0 0
  1. #include "C:\Lib\opencv-release\include\opencv2\opencv.hpp"
  2. #include "C:\Lib\opencv-release\include\opencv2\highgui.hpp"
  3. #include "C:\Lib\opencv-release\include\opencv2\imgproc.hpp"
  4. #include "C:\Lib\opencv-release\include\opencv2\core.hpp"
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <vector>
  9.  
  10. /*
  11. Autoren: Lennart Buchtzik, Dania Blum, Tim Schnakenberg
  12. */
  13.  
  14. using namespace cv;
  15. using namespace std;
  16.  
  17. int main()
  18. {
  19.     //Bild einlesen, Variablen deklarieren
  20.     Mat src = imread("C:/Users/Dania/Pictures/PetraJordan.jpg");
  21.     Mat gray_image;
  22.     Mat blur_image;
  23.     Mat erode_image;
  24.     Mat thold_image;
  25.     Mat dil_image;
  26.  
  27.     //Bild in Grauwertbild umwandeln
  28.     cvtColor(src, gray_image, CV_BGR2GRAY);
  29.  
  30.     //Blur-Effekt auf das Graubild anwenden
  31.     for(int i = 1; i < 8; i++)
  32.     {
  33.         blur(gray_image, blur_image, Size(i,i));
  34.     }
  35.  
  36.     //Threshold-Bild erzeugen
  37.     threshold(gray_image, thold_image, 95.0, 255.0, CV_THRESH_BINARY);
  38.  
  39.     //Erode-Bild erzeugen
  40.     int erosion_size = 1;
  41.     Mat element = getStructuringElement(MORPH_ERODE, Size(2*erosion_size+1, 2*erosion_size+1), Point(erosion_size, erosion_size));
  42.     erode(thold_image, erode_image, element);
  43.  
  44.     //Dilation-Bild erzeugen
  45.     int dilation_size = 1;
  46.     Mat dilElement = getStructuringElement(MORPH_DILATE, Size(2*dilation_size+1, 2*dilation_size+1), Point(dilation_size, dilation_size));
  47.     dilate(thold_image, dil_image, dilElement);
  48.  
  49. //---------------AUFGABE 2--------------------------------------------------------------------------------
  50.  //Parameter
  51.     int histSize = 256;
  52.     float range[] = { 0, 255 };  //Grauwerte gehen von 0 bis 255
  53.     const float *ranges[] = { range };
  54.  
  55.  
  56.     // Histogram berechnen
  57.     MatND hist;
  58.     calcHist( &gray_image, 1, 0, Mat(), hist, 1, &histSize, ranges, true, false );
  59.  
  60.     //ausgabe des Histograms in Command und Berechnung von Schwarz und Weiß Anteilen für 2B
  61.     double total;
  62.     int thresh_v = 95;
  63.     int black = 0;
  64.     int white = 0;
  65.     total = gray_image.rows * gray_image.cols;
  66.     for( int h = 0; h < histSize; h++ ){
  67.  
  68.             if (h<thresh_v){
  69.                 float binVal = hist.at<float>(h);
  70.                 black = black += binVal;
  71.                 cout<<" "<<binVal;
  72.             } else {
  73.                 float binVal = hist.at<float>(h);
  74.                 white = white += binVal;
  75.                 cout<<" "<<binVal;
  76.             }
  77.     }
  78.  
  79.  
  80.     // Histogram ausgeben lassen
  81.     int hist_w = 512; int hist_h = 400;  //Größe des Fensters
  82.     int bin_w = cvRound( (double) hist_w/histSize );
  83.  
  84.     Mat histImage( hist_h, hist_w, CV_8UC1, Scalar( 0,0,0) );
  85.     normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
  86.  
  87.     for( int i = 1; i < histSize; i++ )
  88.     {
  89.       line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) ,
  90.                        Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ),
  91.                        Scalar( 255, 0, 0), 2, 8, 0  );
  92.     }
  93.  
  94.  
  95. //_______________Aufgabe 2b_________________________________________________________________
  96.  
  97.  
  98.  
  99.     double count_ges = 0;
  100.     double black_proz = 0;
  101.     double white_proz = 0;
  102.  
  103.     count_ges = black + white;
  104.  
  105.     black_proz = (black / count_ges) * 100;
  106.     white_proz = (white / count_ges) * 100;
  107.  
  108.  
  109.     cout<<endl;
  110.     cout<<"Werte nach Threshold "<< thresh_v << " :" <<endl;
  111.     cout<<"Pixel die schwarz sind:  "<< black<<endl;
  112.     cout<<"Pixel die weiss sind:  "<< white<<endl;
  113.     cout<<"Prozentuale Ausgabe: "<<endl;
  114.     cout<<"Schwarz: " << black_proz <<endl;
  115.     cout<<"Weiss: " << white_proz <<endl;
  116.  
  117.  
  118. //---------------ALLE AUSGABEN---------------------------------------------------------------------------
  119.  
  120.     //Ausgeben der Bilder
  121.     namedWindow("Image", CV_WINDOW_AUTOSIZE);
  122.     imshow("Image", src);
  123.  
  124.  
  125.     namedWindow("BlurImage", CV_WINDOW_AUTOSIZE);
  126.     imshow("BlurImage", blur_image);
  127.  
  128.     namedWindow("ThresholdImage", CV_WINDOW_AUTOSIZE);
  129.     imshow("ThresholdImage", thold_image);
  130.  
  131.     namedWindow("ErodeImage", CV_WINDOW_AUTOSIZE);
  132.     imshow("ErodeImage", erode_image);
  133.  
  134.     namedWindow("DilatedImage", CV_WINDOW_AUTOSIZE);
  135.     imshow("DilatedImage", dil_image);
  136.  
  137.  
  138.     namedWindow( "Result", 1 );
  139.     imshow( "Result", histImage );
  140.  
  141.     waitKey(0);
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement