Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // OpenCVApplication.cpp : Defines the entry point for the console application.
- // Laborator PI, Nagy Imola, gr 30234
- #include "stdafx.h"
- #include "common.h"
- #include <queue>
- #include <random>
- #include <vector>
- /* 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; i<hist_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);
- }
- Mat rgb_to_yuv(Mat img) {
- Vec3b color;
- Mat yuv = Mat(img.size(), CV_8UC3);
- for (int i = 0; i < img.rows; i++) {
- for (int j = 0; j < img.cols; j++) {
- color = img.at<Vec3b>(i, j); //bgr
- yuv.at<Vec3b>(i, j)[0] = color[2] * .299000 + color[1] * .587000 + color[0] * .114000;
- yuv.at<Vec3b>(i, j)[1] = color[2] * -.168736 + color[1] * -.331264 + color[0] * .500000 + 128;
- yuv.at<Vec3b>(i, j)[2] = color[2] * .500000 + color[1] * -.418688 + color[0] * -.081312 + 128;
- }
- }
- return yuv;
- }
- Mat normalize_yuv(Mat img) {
- Mat yuv_normalized = Mat(img.size(), CV_8UC3);
- yuv_normalized = img.clone();
- for (int i = 0; i < img.rows; i++) {
- for (int j = 0; j < img.cols; j++) {
- yuv_normalized.at<Vec3b>(i, j)[0] = 128;
- float k;
- if (img.at<Vec3b>(i, j)[0] > 128) {
- k = yuv_normalized.at<Vec3b>(i, j)[0] / img.at<Vec3b>(i, j)[0];
- }
- else if (img.at<Vec3b>(i, j)[0] < 128) {
- k = (255 - img.at<Vec3b>(i, j)[0] )/ yuv_normalized.at<Vec3b>(i, j)[0];
- }
- else {
- k = 1;
- }
- yuv_normalized.at<Vec3b>(i, j)[1] = min((img.at<Vec3b>(i, j)[1] - 128) * k + 128, 255);
- yuv_normalized.at<Vec3b>(i, j)[2] = min((img.at<Vec3b>(i, j)[2] - 128) * k + 128, 255);
- }
- }
- return yuv_normalized;
- }
- void test() {
- char fname[MAX_PATH];
- while (openFileDlg(fname)) {
- Mat img = imread(fname, CV_LOAD_IMAGE_UNCHANGED);
- Mat yuv = rgb_to_yuv(img);
- Mat yuv_norm = normalize_yuv(yuv);
- Mat u = Mat(yuv_norm.rows, yuv_norm.cols, CV_8UC1);
- Mat v = Mat(yuv_norm.rows, yuv_norm.cols, CV_8UC1);
- for (int i = 0; i < u.rows; i++) {
- for (int j = 0; j < u.cols; j++)
- {
- u.at<uchar>(i, j) = yuv_norm.at<Vec3b>(i, j)[1];
- v.at<uchar>(i, j) = yuv_norm.at<Vec3b>(i, j)[2];
- }
- }
- imshow("original", img);
- imshow("mod", yuv_norm);
- imshow("u", u);
- imshow("v", v);
- waitKey(0);
- }
- }
- int main()
- {
- test();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement