#include #include #include #include #include "histo.h" using namespace std; using namespace cv; void invertal(Mat img) { img = 255 - img; } void eloterMasolo(const cv::Mat fg, cv::Mat& bg) { bool nincsKep = bg.empty(); bg.create(fg.size(), fg.type()); Vec3b c; Vec3b feher(255, 255, 255); for (int i = 0; i < fg.rows; ++i) { for (int j = 0; j < fg.cols; ++j) { c = fg.at(i, j); if (c[0] < 200 && c[1] < 200 && c[2] < 250) { bg.at(i, j) = c; } else if (nincsKep) { bg.at(i, j) = feher; } } } }; void narancsMasolo(const cv::Mat fg, cv::Mat& bg) { bg = Mat::zeros(fg.size(), fg.type()); Vec3b c; for (int i = 0; i < fg.rows; ++i) { for (int j = 0; j < fg.cols; ++j) { c = fg.at(i, j); if (c[0] < 120 && c[1] < 120 && c[2] < 250) { bg.at(i, j) = c; } } } }; void narancsMasoloHSV(const cv::Mat fg, cv::Mat& bg) { bg = Mat::zeros(fg.size(), fg.type()); Vec3b c; Mat hsv; cvtColor(fg, hsv, COLOR_BGR2HSV); /* for (int i = 0; i < fg.rows; ++i) { for (int j = 0; j < fg.cols; ++j) { c = hsv.at(i, j); if (c[0] < 40 && c[1] < 240 && c[2] < 250) { bg.at(i, j) = fg.at(i,j); } } } */ Mat mask; inRange(hsv, Scalar(0, 0, 0), Scalar(30, 255, 255), mask); fg.copyTo(bg,mask); }; void lilaBarna(const cv::Mat fg) { /* Mat hsv; cvtColor(fg, hsv, COLOR_BGR2HSV); Vec3b c; for (int i = 0; i < fg.rows; ++i) { for (int j = 0; j < fg.cols; ++j) { c = hsv.at(i, j); if (c[0] > 130) { hsv.at(i, j)[0] = 20; } } } */ Mat hsv; cvtColor(fg, hsv, COLOR_BGR2HSV); Mat_ hs = hsv; for (auto& p : hs) { if (130 < p[0]) { p[0] = 20; } } cvtColor(hsv, fg, COLOR_HSV2BGR); } void mormotaMasolo(const cv::Mat fg, cv:: Mat bg) { Mat hsv; cvtColor(fg, hsv, COLOR_BGR2HSV); Mat mask; inRange(hsv, Scalar(100, 0, 0), Scalar(130, 255, 255), mask); mask = 255 - mask; cvtColor(hsv, fg, COLOR_HSV2BGR); Rect r(0, (bg.rows - 1) - fg.rows,fg.cols,fg.rows); fg.copyTo(bg(r), mask); } void convert(const Mat img, Mat& lab) { Mat imgf; img.convertTo(imgf,CV_32FC3,1/255.0); cvtColor(imgf,lab, COLOR_BGR2Lab); } double totaldiff(const Mat elab, const Mat lab) { Mat diff = elab - lab; Mat diff2 = diff.mul(diff); vector chs; split(diff2, chs); Mat E; cv::sqrt(chs[0] + chs[1] + chs[2], E); return mean(E)[0]; } void createHisto(const Mat img, Mat& hiszto) { //a hiszto az eredmeny, float típusú elemeket tartalmaz majd vector kepek; kepek.push_back(img); // egy képet használunk vector csatornak; csatornak.push_back(0); //a képnek a 0. csatornáját használjuk vector hiszto_meretek; hiszto_meretek.push_back(256); //szürkeárnyalatok száma vector hiszto_tartomanyok; hiszto_tartomanyok.push_back(0.0f); //hol kezdődik a tartomány hiszto_tartomanyok.push_back(255.f); //meddig tart //accumlate: marad false (nullázza a hisztogrammot) calcHist(kepek, csatornak, noArray(), hiszto, hiszto_meretek, hiszto_tartomanyok, false); } int calcThreshold(const Mat img, float ratio = 0.1f) { Mat hist; Histo::calcHistoC1(img, hist); int numPixels = img.rows * img.cols * ratio; float s = 0; for (int th = 0; th <= 255; ++th) { s += hist.at(th); if (s >= numPixels) { return th; } } return -1; } int main() { /*cv::Mat img = imread("c:/Users/student/Downloads/panda.jpg"); Mat img = Mat::zeros(96,203,CV_8UC3); img.setTo(Scalar(0,0,255)); imshow("kep",img); Mat img2(img.size(), img.type()); Mat sekely = img; Mat mely; img.copyTo(mely); img.setTo(255); imshow("kep", img); imshow("sekely", sekely); imshow("mely", mely); Mat a(400, 600, CV_8UC3); a.setTo(Scalar(0,0,255)); Rect rect(0, 0, a.cols, a.rows/2); Mat b = a(rect); b.setTo(Scalar(0, 255, 255)); imshow("ROI", a); Mat kurama = imread("c:/Users/student/Downloads/kurama.jpg", IMREAD_COLOR); int size = 20; Rect kuramaCropRect(size, size, kurama.cols - 2*size, kurama.rows - 2*size); Mat kuramaCrop = kurama(kuramaCropRect); //imshow("kurama", kurama); //imshow("kuramaCrop", kuramaCrop); //imwrite("kuramaCrop.png",kuramaCrop); Mat kurama = imread("c:/Users/student/Downloads/kurama.jpg", IMREAD_COLOR); Mat bg = imread("c:/Users/student/Downloads/background.jpg", IMREAD_COLOR); resize(kurama,kurama,Size(200,200)); imshow("kurama", kurama); Rect r(bg.cols / 2 - 100, bg.rows / 2 - 100,200,200); kurama.copyTo(bg(r)); imshow("bg",bg); //Mat bg = imread("c:/Users/student/Downloads/background.jpg",IMREAD_GRAYSCALE); //Mat kurama = imread("c:/Users/student/Downloads/kurama.jpg",IMREAD_GRAYSCALE); //Mat dest = bg + 50; if (kurama.empty()) { cout << "nincs kep" << endl; return -1; } invertal(kurama); imshow("bg0",bg); imshow("bg1", dest); imshow("kurama", kurama); Mat bg = imread("c:/Users/student/Downloads/background.jpg", IMREAD_COLOR); Mat kurama = imread("c:/Users/student/Downloads/kurama.jpg", IMREAD_COLOR); resize(bg, bg, kurama.size()); double alfa = 0.5f; Mat dest = alfa * kurama + (1 - alfa) * bg; imshow("mix", dest); Mat bg = imread("c:/Users/student/Downloads/background.jpg", IMREAD_COLOR); Mat kurama = imread("c:/Users/student/Downloads/kurama.jpg", IMREAD_COLOR); eloterMasolo(kurama, bg); imshow("bg", bg); Mat narancs = imread("c:/Users/student/Downloads/orange1.jpg", IMREAD_COLOR); Mat bg; narancsMasoloHSV(narancs, bg); imshow("bg",bg); Mat milka = imread("c:/Users/student/Downloads/milka.jpg",IMREAD_COLOR); lilaBarna(milka); imshow("milka", milka); Mat mormota = imread("c:/Users/student/Downloads/mormota_kekhatter.jpg"); Mat deik = imread("c:/Users/student/Downloads/deik.jpg"); mormotaMasolo(mormota, deik); imshow("deik", deik); Mat bg = imread("c:/Users/student/Downloads/Deik.jpg", IMREAD_COLOR); Mat lab; convert(bg, lab); Vec3f p = lab.at(0, 0); Vec3f p2 = lab.at(0, 1); cout << norm(p - p2) << endl; Mat etalon = imread("C:/Users/student/Downloads/padlolapok/etalon.png", IMREAD_COLOR); Mat elab; convert(etalon, elab); imshow("etalon", etalon); moveWindow("etalon", 400, 100); for (int i = 1; i < 8; i++) { Mat img = imread("C:/Users/student/Downloads/padlolapok/fa_" + to_string(i) + ".png"); Mat lab; convert(img, lab); string name = to_string(i); imshow(name, img); moveWindow(name,800,100+80*i); double d = totaldiff(elab,lab); cout << i << " " << d << endl; } Mat img = imread("C:/Users/student/Downloads/debrecen_deep.png", IMREAD_GRAYSCALE); imshow("img", img); Histo::showHisto(img,"eredeti",1); double ah, fh; minMaxLoc(img,&ah,&fh); Mat dest = (img - ah) * (255.0 / (fh - ah)); imshow("dest", dest); Histo::showHisto(dest, "eredmény", 1); Mat img = imread("C:/Users/student/Downloads/dark_img.jpg", IMREAD_GRAYSCALE); imshow("img", img); Histo::showHisto(img, "eredeti", 1); Mat equalised; equalizeHist(img,equalised); imshow("equalised", equalised); double ah, fh; minMaxLoc(img, &ah, &fh); fh = 30; Mat dest = (img - ah) * (255.0 / (fh - ah)); imshow("dest", dest); Histo::showHisto(dest, "eredmény", 1); Mat img = imread("C:/users/student/Downloads/orange1.jpg", IMREAD_COLOR); imshow("img", img); Histo::showHisto(img, "eredeti", 1); Mat hsv; cvtColor(img, hsv, COLOR_BGR2HSV); vector chs; split(hsv, chs); equalizeHist(chs[2],chs[2]); Mat dest; merge(chs,dest); cvtColor(dest, dest, COLOR_HSV2BGR); imshow("dest", dest); Histo::showHisto(dest, "eredmény", 1); Mat img = imread("C:/Users/student/Downloads/szines.png",IMREAD_COLOR); string menu = "tool"; cv::namedWindow(menu, WINDOW_NORMAL); cv::resizeWindow(menu, Size(500, 50)); int radius = 1, sigma = 1; createTrackbar("radius", menu, &radius, 25); createTrackbar("sigma", menu, &sigma, 25); imshow("src", img); Mat blur_img, gauss_img, median_img, bilat_img; while (cv::waitKey(20) != 'q') { //q-ra lép ki int size = max(2 * radius + 1, 3); Size size2(size, size); blur(img,blur_img, size2); imshow("blur",blur_img); GaussianBlur(img, gauss_img, size2, sigma, sigma); imshow("gauss",gauss_img); medianBlur(img, median_img, size); imshow("median", median_img); bilateralFilter(img,bilat_img,5, sigma*10, 2 * radius + 3); imshow("bilat", bilat_img); //mossa el a beolvasott kepet a kulonbozo szurokkel //a szuro merete legyen: 2*radius+3 vagy Size(2*radius+3, 2*radius+3) //a BilateralFilter-nel: //d: 5 //sigmaColor : sigma * 10 //csak, hogy latvanyos legyen //sigmaSpatial : 2 * radius + 3 //jelenitse meg a kepeket } Mat img = imread("C:/Users/student/Downloads/KossuthSquare/SnapShot-20180731_173715.jpg", IMREAD_COLOR); imshow("img", img); int num_frame = 0; Mat acc = Mat::zeros(img.size(), CV_64FC3); for (int i = 715; i <= 918; i++) { //918 img = imread("C:/Users/student/Downloads/KossuthSquare/SnapShot-20180731_173" + to_string(i) + ".jpg",IMREAD_COLOR); if (!img.empty()) { acc += img; num_frame++; imshow("Kossuth", img); } waitKey(25); } acc /= num_frame; Mat dest; acc.convertTo(dest, CV_8UC3); imshow("dest", dest); if (false) { Mat img = imread("C:/Users/student/Downloads/dog.jpg", IMREAD_GRAYSCALE); imshow("img", img); Mat dest; threshold(img, dest, 100, 255, THRESH_BINARY); imshow("dest", dest); medianBlur(dest, dest, 3); imshow("kuszobolt", dest); } Mat img = imread("C:/Users/student/Downloads/madar.jpg", IMREAD_COLOR); imshow("img", img); vector chs; split(img, chs); imshow("R", chs[2]); imshow("G", chs[1]); imshow("B", chs[0]); Mat dest; threshold(chs[0], dest, 100, 255, THRESH_BINARY); imshow("zajos kuszobolt", dest); Mat img = imread("C:/Users/student/Downloads/gray_buttons.jpg", IMREAD_GRAYSCALE); imshow("img", img); Mat dest; inRange(img,165,213,dest); imshow("zajos", dest); medianBlur(dest, dest, 5); imshow("szurt", dest); img.setTo(0, 255 - dest); imshow("savkivagott", img); */ //VideoCapture cap; /*cap.open("C:/Users/student/Downloads/sas.avi"); if (!cap.isOpened()) { cout << "hiba" << endl; return -1; } Mat img, gray, egmask, dest; while (true) { cap >> img; if (img.empty()) { break; } cvtColor(img, gray, COLOR_BGR2GRAY); inRange(gray,100,155,egmask); dest = img.clone(); dest.setTo(Scalar(0, 0, 0), egmask); imshow("lejatszo", dest); waitKey(5); } */ /* Mat img = imread("C:/Users/student/Downloads/szitakoto.jpg",IMREAD_GRAYSCALE); Mat se = getStructuringElement(MORPH_ELLIPSE, Size(5, 9)); Mat mask, eroded, dest, dilated; threshold(img, mask, 120, 255, THRESH_BINARY_INV); imshow("mask", mask); Mat dest2; morphologyEx(mask, dest2, MORPH_OPEN, se); imshow("dest", dest2); Rect r(img.cols / 3, 0, img.cols - img.cols / 3, img.rows); Mat folt = dest2(r); imshow("folt", folt); */ /* Mat img = imread("C:/Users/student/Downloads/annotalt_sejt.png",IMREAD_COLOR); vector chs; split(img, chs); imshow("ch[2]", chs[2]); Mat mask; threshold(chs[2], mask, 254, 255, THRESH_BINARY); Mat se = getStructuringElement(MORPH_CROSS, Size(3,3)); Mat dest; morphologyEx(mask, dest, MORPH_HITMISS, se); vector pts; for (int i = 0; i < img.rows; ++i) { for (int o = 0; o < img.cols; ++o) { if (dest.at(i, o)) { pts.push_back(Point(o, i)); } } } Mat img2 = img.clone(); for (auto p : pts) { drawMarker(img2, p, Scalar(0, 255, 0), MARKER_DIAMOND, 10, 2); } imshow("jelolok", img2); */ /* Mat img = imread("C:/Users/student/Downloads/graycat.jpg", IMREAD_COLOR); imshow("eredeti", img); Mat se = getStructuringElement(MORPH_ELLIPSE, Size(5,5)); Mat img2; erode(img, img2, se); imshow("erodalt", img2); dilate(img, img2, se); imshow("dilat", img2); */ /* Mat_ img = imread("C:/Users/student/Downloads/graycat.jpg",IMREAD_GRAYSCALE); imshow("eredeti", img); Mat_ dest = Mat::zeros(img.size(), CV_8UC1); for (int i = 0; i < img.cols; ++i) { for (int o = 0; o < img.rows; ++o) { dest(i, o) = abs(img(i, o) - img(i + 1, o + 1)) + abs(img(i, o + 1) - img(i + 1, o)); } } imshow("grad", dest); */ /* Mat_ img = imread("C:/Users/student/Downloads/graycat.jpg", IMREAD_GRAYSCALE); imshow("eredeti", img); Mat grad1, grad2; Sobel(img, grad1, CV_32F, 1, 0); Sobel(img, grad2, CV_32F, 0, 1); //gradiens approximacio Mat grad = cv::abs(grad1) + cv::abs(grad2); Mat grad3; cv::sqrt(grad1.mul(grad1) + grad2.mul(grad2),grad3); Mat dest; convertScaleAbs(grad3, dest); //imshow("grad1", grad1 * 255); //imshow("grad2", grad2 * 255); //imshow("grad", grad * 255); imshow("grad pontos", dest); Mat dest2; threshold(dest,dest2, 15, 255, THRESH_BINARY); imshow("el", dest2); Laplacian(img, dest, CV_16S, 3); convertScaleAbs(dest, dest); imshow("dest", dest); */ Mat_ img = imread("C:/Users/student/Downloads/go2.png", IMREAD_GRAYSCALE); imshow("eredeti", img); //Mat dest; //Canny(img, dest, 50, 160); vector circles; //cx, cy, r HoughCircles(img, circles, HOUGH_GRADIENT, 2, 10, 80, 50, 22, 26); cout << circles.size() << endl; Mat dest; cvtColor(img, dest, COLOR_GRAY2BGR); for (Vec3f c : circles) { if (img.at(c[1], c[0]) > 128) { circle(dest, Point(c[0], c[1]), c[2], Scalar(0,0,255), 2); } else { circle(dest, Point(c[0], c[1]), c[2], Scalar(255, 0, 0), 2); } } imshow("korok", dest); waitKey(0); return 0; }