Assignment Title

CS 585 HW 1
Wang Kaihong
9/13/2018


Problem Definition


Method and Implementation


Experiments

Expriment was operated on a 64-bit OS PC equipped with Intel(R) Core(TM) i5-5257U CPU@2.70GHz and 2GB RAM.
Image used in this expriment is a face image of Wang Kaihong.


Code

#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int safe_index(Mat m,int row, int col,int channel,int max_rows,int max_cols) {
if (col>=0 && col<max_cols && row>=0 && row<max_rows) {
return (int)m.at<Vec3b>(row,col)[channel];
}
else {
return 0;
}
}
int main()
{
Mat image;
image = imread("me.jpg", IMREAD_COLOR); // Read the file
if (image.empty()) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
return 0;
}
Mat gray_image = Mat(image.rows,image.cols,image.type());
//namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
//imshow("Display window", image); // Show our image inside it.
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
int m = (int)((int)image.at<Vec3b>(i, j)[0] + (int)image.at<Vec3b>(i, j)[1] + (int)image.at<Vec3b>(i, j)[2]) / 3;
gray_image.at<Vec3b>(i, j)[0] = m;
gray_image.at<Vec3b>(i, j)[1] = m;
gray_image.at<Vec3b>(i, j)[2] = m;
}
}
namedWindow("Display window1", WINDOW_AUTOSIZE); // Create a window for display.
imshow("Display window1", gray_image); // Show our image inside it.
imwrite("gray_me.jpg", gray_image);
Mat filp_image = Mat(image.rows, image.cols, image.type());
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
filp_image.at<Vec3b>(i, j)[0] = (int)image.at<Vec3b>(i, image.cols - j - 1)[0];
filp_image.at<Vec3b>(i, j)[1] = (int)image.at<Vec3b>(i, image.cols - j - 1)[1];
filp_image.at<Vec3b>(i, j)[2] = (int)image.at<Vec3b>(i, image.cols - j - 1)[2];
}
}
namedWindow("Display window2", WINDOW_AUTOSIZE); // Create a window for display.
imshow("Display window2", filp_image); // Show our image inside it.
imwrite("flip_me.jpg", filp_image);
Mat blur_image = Mat(image.rows, image.cols, image.type());
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
blur_image.at<Vec3b>(i, j)[0] = safe_index(image, i - 1, j - 1, 0, image.rows, image.cols) + safe_index(image, i, j - 1, 0, image.rows, image.cols) + safe_index(image, i + 1, j - 1, 0, image.rows, image.cols) + safe_index(image, i - 1, j, 0, image.rows, image.cols) + safe_index(image, i, j, 0, image.rows, image.cols) + safe_index(image, i + 1, j, 0, image.rows, image.cols) + safe_index(image, i - 1, j + 1, 0, image.rows, image.cols) + safe_index(image, i, j + 1, 0, image.rows, image.cols) + safe_index(image, i + 1, j + 1, 0, image.rows, image.cols);
blur_image.at<Vec3b>(i, j)[1] = safe_index(image, i - 1, j - 1, 1, image.rows, image.cols) + safe_index(image, i, j - 1, 1, image.rows, image.cols) + safe_index(image, i + 1, j - 1, 1, image.rows, image.cols) + safe_index(image, i - 1, j, 1, image.rows, image.cols) + safe_index(image, i, j, 1, image.rows, image.cols) + safe_index(image, i + 1, j, 1, image.rows, image.cols) + safe_index(image, i - 1, j + 1, 1, image.rows, image.cols) + safe_index(image, i, j + 1, 1, image.rows, image.cols) + safe_index(image, i + 1, j + 1, 1, image.rows, image.cols);
blur_image.at<Vec3b>(i, j)[2] = safe_index(image, i - 1, j - 1, 2, image.rows, image.cols) + safe_index(image, i, j - 1, 2, image.rows, image.cols) + safe_index(image, i + 1, j - 1, 2, image.rows, image.cols) + safe_index(image, i - 1, j, 2, image.rows, image.cols) + safe_index(image, i, j, 2, image.rows, image.cols) + safe_index(image, i + 1, j, 2, image.rows, image.cols) + safe_index(image, i - 1, j + 1, 2, image.rows, image.cols) + safe_index(image, i, j + 1, 2, image.rows, image.cols) + safe_index(image, i + 1, j + 1, 2, image.rows, image.cols);
}
}
namedWindow("Display window3", WINDOW_AUTOSIZE); // Create a window for display.
imshow("Display window3", blur_image); // Show our image inside it.
imwrite("blur_me.jpg", blur_image);
waitKey(0); // Wait for a keystroke in the window
system("pause");
return 0;
}



Results

Results

Trial Source Image Result Image
grayscale image
flip image
sum up image


Discussion


Conclusions

All three processed images achieve expectation of our expriment.