0% found this document useful (0 votes)
9 views21 pages

Oop Assign 2

The document outlines the code for an image editing program in C++ using OpenCV, including functions to load, display, resize, rotate, crop, adjust brightness/contrast, and apply filters to an image. It defines an ImageEdit class to handle image editing operations and a menu function to allow the user to select different editing options by entering a number choice. The code takes a user-selected image file, loads it, and allows the user to repeatedly edit and save the modified image until exiting the program.

Uploaded by

Adnan Aijaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views21 pages

Oop Assign 2

The document outlines the code for an image editing program in C++ using OpenCV, including functions to load, display, resize, rotate, crop, adjust brightness/contrast, and apply filters to an image. It defines an ImageEdit class to handle image editing operations and a menu function to allow the user to select different editing options by entering a number choice. The code takes a user-selected image file, loads it, and allows the user to repeatedly edit and save the modified image until exiting the program.

Uploaded by

Adnan Aijaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

CS-212 Object Oriented Programming

ASSIGNMENT- 02
Course Instructor: Lecturer Anum Abdul Salam

Student Name: _____Syed Adnan Aijaz Bukhari

Degree/ Syndicate: ______________________CE-44-B_______________

1
Task : Open cv along with output of code

Main Code:

// Include necessary OpenCV and C++ headers


#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace std;


using namespace cv;

// Path to the default directory for image operations


const String path = "E:/Semesters/3rd/OOP/cvtest";

// Class for image editing operations


class ImageEdit {
string file; // File name
Mat orig_image; // Original image
Mat edit_image; // Edited image
bool flag; // Flag to track editing state

public:
// Load image from file
Mat ImageLoad(const string& Path) {
return imread(Path);
}

// Save image to file


void saveImage(const Mat& image, const string& Path) {
imwrite(Path, image);
}

// Check if the image is empty


bool empty(const Mat& image) {
return image.empty();
}

// Display the image in a window


void display(const Mat& image, const string& windowName ) {
if (empty(image)) {
cout << "Image is empty" << endl;
}

2
else {
cout << "Image : " << endl;
imshow(windowName, image);
waitKey(0);
destroyAllWindows();
}
}

// Resize the image


void ResizeImage(const Mat& image) {
cout << "Original Height and Width: " << image.rows << "x" << image.cols
<< endl;
int size_width;
int size_height;
Mat resizeImage;
do {
cout << "Enter the resize image width: ";
cin >> size_width;
} while (size_width <= 10 || size_width >= 3000);
do {
cout << "Enter the resize image height: ";
cin >> size_height;
} while (size_height <= 10 || size_height >= 3000);
resize(image, resizeImage, Size(size_width, size_height), 1, 0,
INTER_LANCZOS4);
cout << "Resize Image Height and Width: " << resizeImage.rows << "x" <<
resizeImage.cols << endl;
display(resizeImage, "Resized Image ");
EditSavePrompt(resizeImage);
}

// Rotate the image


void RotateImage(const Mat& image) {
float angle;
Mat rotated;
cout << "Enter Angle of rotation: ";
cin >> angle;
Mat editimage = image.clone();

Point2f center(double(editimage.cols) * 0.5, double(editimage.rows) *


0.5);
Mat rotationMatrix = getRotationMatrix2D(center, angle, 1.0);

// Calculate bounding box and adjust center


Rect bbox = RotatedRect(center, editimage.size(), angle).boundingRect();

3
rotationMatrix.at<double>(0, 2) += bbox.width / 2.0 - center.x;
rotationMatrix.at<double>(1, 2) += bbox.height / 2.0 - center.y;

warpAffine(editimage, rotated, rotationMatrix, bbox.size());


// Display the rotated image
display(rotated, "Rotated Image ");
// Prompt for saving the edited image
EditSavePrompt(rotated);
}

// Crop the image


void CropImage(const Mat& image) {
if (!image.empty()) {
bool correct = false;
do {
int x, y, width, height;

cout << "Enter the x-axis of crop region: ";


cin >> x;
cout << "Enter the y-axis of crop region: ";
cin >> y;
cout << "Enter the Width of crop region: ";
cin >> width;
cout << "Enter the height of crop region: ";
cin >> height;

if (x >= 0 && y >= 0 && (x + width) <= edit_image.cols && (y +


height) <= edit_image.rows) {
Rect roi(x, y, width, height);
Mat cropped = edit_image(roi).clone();
cout << "Crop Region Height and Width: " << cropped.rows <<
"x" << cropped.cols << endl;
display(cropped, "Cropped Image ");
EditSavePrompt(cropped);
correct = true;
}
else {
cout << "Invalid crop region. Ensure the coordinates and
dimensions are within the image bounds." << endl;
}
} while (correct == false);
}
else {
cout << "Image is empty. " << endl;
}

4
}

// Sharpen the image


void sharpen(Mat& FilterImage) {
Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(FilterImage, FilterImage, FilterImage.depth(), kernel);
display(FilterImage, "Sharpened Image");
EditSavePrompt(FilterImage);
}

// Apply various filters to the image


void ApplyFilter(const Mat& image) {
int choice;
Mat filterImage = image;
do {
cout << "Press 1: To Apply Grayscale Filter" << endl
<< "Press 2: To Apply Blur Filter" << endl
<< "Press 3: To Apply Sharpen Filter" << endl
<< "Press 4: To exit function" << endl;
cin >> choice;

switch (choice) {
case 1:
cvtColor(filterImage, filterImage, COLOR_BGR2GRAY);
display(filterImage, "Grayscale Image");
EditSavePrompt(filterImage);
break;
case 2:
int kernelSize;
do {
cout << "Enter kernel size for blur filter (it should be odd
and maximum kernelSize is 31): ";
cin >> kernelSize;
} while (kernelSize % 2 != 1 || kernelSize > 31 || kernelSize <
0);
GaussianBlur(filterImage, filterImage, Size(kernelSize,
kernelSize), 0);
display(filterImage, "Blurred Image");
EditSavePrompt(filterImage);
break;
case 3:
sharpen(filterImage);
break;
case 4:
break;

5
default:
cout << "Invalid choice. Try again." << endl;
}
} while (choice != 4);
}

// Adjust the brightness and contrast of the image


void ContrastAdjustment(const Mat& image) {
double alpha = 0, beta = 0;
Mat adjusted;
int choice;
do {
cout << "Press 1: To Adjust Brightness of Image only " << endl
<< "Press 2: To Adjust Contrast of Image only " << endl
<< "Press 3: To Adjust both the Brightness and Contrast of
Image " << endl
<< "Press 4: To exit function " << endl;
cin >> choice;
if (choice==1){
cout << "Enter the brightness (beta) value (e.g., 0 for no
change): ";
cin >> beta;
image.convertTo(adjusted, -1, 1, beta); // Adjusting brightness
only (alpha = 1)
display(adjusted, "Brightness Adjusted Image");
EditSavePrompt(adjusted);
}
else if (choice == 2) {
cout << "Enter the contrast (alpha) value (e.g., 1.0 for no
change): ";
cin >> alpha;
image.convertTo(adjusted, -1, alpha, 0); // Adjusting contrast
only (beta = 0)
display(adjusted, "Contrast Adjusted Image");
EditSavePrompt(adjusted);
}
else if (choice == 3) {
cout << "Enter the brightness (beta) value (e.g., 0 for no
change): ";
cin >> beta;
cout << "Enter the contrast (alpha) value (e.g., 1.0 for no
change): ";
cin >> alpha;
image.convertTo(adjusted, -1, alpha, beta);
display(adjusted, "Brightness and Contrast Adjusted Image");

6
EditSavePrompt(adjusted);
}
else if (choice == 4) {
break;
}
} while (choice >= 1 && choice <= 4);
}

// Take user input for file name


void input(string& file) {
cout << "Enter File Name: ";
cin >> file;
extension(file);
}

// Determine the file extension


void extension(string& file) {
int choice;
cout << "The File type:" << endl
<< "Press 1 For PNG" << endl
<< "Press 2 For JPG" << endl
<< "Press 3 For WebP" << endl
<< "Enter your choice: ";
cin >> choice;
do {
switch (choice) {
case 1:
file = path + "/" + file + ".png";
break;
case 2:
file = path + "/" + file + ".jpg";
break;
case 3:
file = path + "/" + file + ".webp";
break;
default:
cout << "Invalid Input: " << endl;
choice = 0;
}
} while (choice == 0);
}

// Main menu for image editing options


int menu() {
cout << file << endl;

7
input(file);
orig_image = ImageLoad(file);
flag = true;
int choice;
int option;
if (orig_image.empty()) {
cerr << "Error: Could not open or find the image." << endl;
return -1;
}
cout << "Welcome to image Editor: " << endl;
do {
cout << "Press 1: To resize Image " << endl
<< "Press 2: To Rotate Image " << endl
<< "Press 3: To Crop Image" << endl
<< "Press 4: To Adjust Image Brightness and Contrast " << endl
<< "Press 5: To Apply Filters on Image " << endl
<< "Press 6: To save Image " << endl
<< "Press 7: To display Image " << endl
<< "Press 8: To exit the Image Editor " << endl;
cin >> choice;
switch (choice) {
case 1:
if (flag) ResizeImage(orig_image);
else ResizeImage(edit_image);
break;
case 2:
if (flag) RotateImage(orig_image);
else RotateImage(edit_image);
break;
case 3:
if (flag) CropImage(orig_image);
else CropImage(edit_image);
break;
case 4:
if (flag) ContrastAdjustment(orig_image);
else ContrastAdjustment(edit_image);
break;
case 5:
if (flag) ApplyFilter(orig_image);
else ApplyFilter(edit_image);
break;
case 6:
do {
cout << "Press 1: To Save the original image." << endl
<< "Press 2: To save the edited image." << endl

8
<< "Enter your Choice: ";
cin >> option;
} while (option < 1 || option > 2);
if (option == 1) {
cout << "Press 1: To Save the Image by its name." << endl
<< "Press 2: To save the image by a new name." << endl
<< "Enter your Choice: ";
cin >> option;
SavePrompt(option, orig_image);
}
else if (option == 2) {
if (edit_image.empty()) {
cerr << "Error: Could not open or find the image." <<
endl;
return -1;
}
else {
cout << "Press 1: To Save the Edited Image in place of
the original image." << endl
<< "Press 2: To save the Edited image." << endl
<< "Enter your Choice: ";
cin >> option;
SavePrompt(option, edit_image);
}
}
break;
case 7:
if (flag) display(orig_image, "Preview");
else display(edit_image, "Preview");
break;
case 8:
break;
default:
choice = 0;
}
} while (choice != 8);

return 0;
}

// Prompt for saving edited image


void EditSavePrompt(Mat& image) {
int choice;
Mat empty;

9
do {
cout << "Press 1: To save this Edited Image" << endl
<< "Press 2: To remove this Edited image Only" << endl
<< "Press 3: To remove all Edited Image" << endl;
cin >> choice;

if (choice == 1) {
flag = false;
edit_image = image;
break;
}
else if (choice == 2) {
if (!edit_image.empty()) {
flag = false;
image = edit_image;
}
else {
cout << "No Edit Image Exists." << endl;
flag = true;
}
break;
}
else if (choice == 3) {
flag = true;
edit_image = empty;
break;
}
else if (choice < 1 || choice > 3)
cout << "Invalid choice. Try again." << endl;
} while (choice < 1 || choice > 3);
}

// Prompt for saving image


void SavePrompt(int option, Mat& image) {
string newfile;
do {
if (option == 1) {
saveImage(image, file);
break;
}
else if (option == 2) {
input(newfile);
saveImage(image, newfile);
break;
}

10
else
cout << "Invalid input: " << endl;
} while (option != 0);
}
};

int main() {
ImageEdit image;
return image.menu();
}

Output:

11
12
13
14
15
16
17
18
Saved Image (Edit1.png):

19
UML Diagram:

20
ImageEdit

- file: string

- orig_image: Mat

- edit_image: Mat

- flag: bool

- ImageLoad(const string&): Mat

- saveImage(const Mat&, const string&): void

- empty(const Mat&): bool

- display(const Mat&, const string&): void

- ResizeImage(const Mat&): void

- RotateImage(const Mat&): void

- CropImage(const Mat&): void

- sharpen(Mat&): void

- ApplyFilter(const Mat&): void

- ContrastAdjustment(const Mat&): void

- input(string&): void

- extension(string&): void

- SavePrompt(int, Mat&): void

- EditSavePrompt(Mat&): void

21

You might also like