Click here to Skip to main content
15,947,476 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i am working on a project in C++ WinForms. i have a blur and defocused Image. i want to make that image more focused and sharpened but smooth and clean as this Image. this image enhanced in mobile application. and i want the same result as this image.
if anyone can help or guide then it will be must appreciated!!!

What I have tried:

i have made a WinForms C++ project and using open cv i am trying to make image more detailed focused and sharp. i have used filter2D() and bilateralFilter() function and i am getting the result Image. still this is not the result i want.

here is my source code that i have tried.



C++
void enhanceImage(Mat& inputImage, Mat& outputImage)
		{
			try {
				// Code to enhance image using OpenCV functions goes here
				// For example, sharpening the image:
				Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
				filter2D(inputImage, outputImage, -1, kernel);
				
							
			}
			catch (System::Exception^ex)
			{
				// handle the exception
				MessageBox::Show("An error occurred: " + ex->Message);
			}
		}



C++
void removeNoise(Mat& inputImage, Mat& outputImage)
		{
			try {
				// Apply a bilateral filter to smooth the edges
				bilateralFilter(inputImage, outputImage, 9, 75, 75);
		
			}
			catch (System::Exception^ ex)
			{
				// handle the exception
				MessageBox::Show("An error occurred: " + ex->Message);
			}
		}



C++
private: System::Void EnhanceBn_Click(System::Object^ sender, System::EventArgs^ e)
	{
		try {
			

			string folderPath = msclr::interop::marshal_as<string>(dialog->SelectedPath);
			savedialog->ShowDialog();
			int numFiles = std::distance(std::filesystem::directory_iterator(folderPath), std::filesystem::directory_iterator());

			progressBar->Minimum = 0;
			progressBar->Maximum = numFiles;
			progressBar->Value = 0;

			int i = 0;
			// Loop through all files in the selected folder
			for (const auto& file : filesystem::directory_iterator(folderPath))
			{
				// Load image using OpenCV
				Mat inputImage = imread(file.path().string());

				// Create output image
				Mat outputImage;
				Mat enhancedImage;

				// Enhance image
				enhanceImage(inputImage, enhancedImage);
				removeNoise(enhancedImage, outputImage);
				
				string savefolderPath = msclr::interop::marshal_as<string>(savedialog->SelectedPath);
				// Save enhanced image to new folder
				string outputPath = savefolderPath + "\\" + file.path().filename().string();
				imwrite(outputPath, outputImage);

				i++;
				label->Text = i.ToString();
				progressBar->Value = i;
				Application::DoEvents();
			}
			label->Text = "Processing completed!";
			MessageBox::Show("images enhanced sucessfully");
			progressBar->Value = 0;
			
		}
		catch (System::Exception^ ex)
		{
			// handle the exception
			MessageBox::Show("An error occurred: " + ex->Message);
		}

	}
Posted
Comments
Shao Voon Wong 23-Mar-23 3:07am    
You can try tweaking the kernel and the parameters. But before you do that, please understand what they do first.
[no name] 26-Mar-23 1:20am    
Trail and error. Once you have enough trials, you can use machine learning. (e.g. is it an orange or a orange beach ball)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900