opencv2 bluring images - c++

I'm trying to blur an image , and gaussion blur an image but all that ends up happening when i run my code is the image opens up without bluring. Can anyone help me with this problem?
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main() {
//read the image
cv::Mat image= cv::imread("Space_Nebula.jpg");
cv::Mat result;
// create image window
cv::namedWindow("My Image");
//display image
cv::imshow("My Image", image);
//wait key
cv::waitKey(50000);
//blur image
cv::blur(image,result,cv::Size(5,5));
cv::imshow("My Image", image);
//smooth image
cv::GaussianBlur(image,result,cv::Size(5,5),1.5);
cv::imshow("My Image", image);
return 1;
}

A couple things: You are processing image into the Mat called result but then displaying image. Also, there is no call to waitKey after the last two calls to imshow so you aren't seeing those at all. And a small point: return 0 from main to signal completion with no error. Try this instead:
//read the image
cv::Mat image= cv::imread("../../IMG_0080.JPG");
cv::Mat result;
// create image window
cv::namedWindow("My Image");
//display image
cv::imshow("My Image", image);
//wait key
cv::waitKey(0);
//blur image
cv::blur(image,result,cv::Size(5,5));
cv::imshow("My Image", result);
cv::waitKey(0);
//smooth image
cv::GaussianBlur(image,result,cv::Size(5,5),1.5);
cv::imshow("My Image", result);
cv::waitKey(0);
return 0;

Do cv::imshow("My Image", result); instead of cv::imshow("My Image", image);.

Related

how draw canny result over image?

i want to draw the canni result on the original image. How can i do this? i tried like this, but an error comes out
Mat image;
image = imread("C:\\test.jpg",1);
Mat gray, edqes, out;
cvtColor(image, gray, COLOR_BGR2GRAY);
Canny(gray, edqes, 100, 200, 3);
out.copyTo(image,edqes);
cvNamedWindow("original",CV_WINDOW_NORMAL);
cvNamedWindow("binary",CV_WINDOW_NORMAL);
cvNamedWindow("canny",CV_WINDOW_NORMAL);
cvNamedWindow("out",CV_WINDOW_NORMAL);
imshow("original",image);
imshow("binary", gray);
imshow("canny", edqes);
imshow("out", out);
cvWaitKey(0);
cvDestroyAllWindows();
Try:
cvtColor(edqes, edqes, COLOR_GRAY2BGR);
bitwise_or(edqes,image,out);

Image Quality Distorted after convering Mat image to QImage

void Qtexample::on_pushButton_clicked()
{
QString img_path = QFileDialog::getOpenFileName(this, "Select your image", QDir::homePath());
img = imread(img_path.toStdString().c_str());
Size size(200, 200);
cv::resize(img, img,size);
cvtColor(img, img, COLOR_RGB2BGR); //convering the image to RGB for QImage to handle well
QImage Qimg((uchar*)img.data, img.cols, img.rows, img.step, QImage::Format_RGB888); // converting Mat image to QImage
ui.label->setPixmap(QPixmap::fromImage(Qimg,Qt::AutoColor).scaled(ui.label->width(), ui.label->height())); // displaying the image inside the QLabel with the help of QPixmap
// imshow("image", img); // show image
}
the code works by converting a Mat image to QImage but the resulted Image is distorted a little and I need help on it.

proximate contour to rectagle

I'm trying to detect and extract playing cards from an image. The plan is to detect the contours of the cards and then extract them using the area of the contours. (Is there a more efficient way of doing it?)
The problem is that I was having trouble with non-closed contours:
With this contours I'm not able to calculate the area of the rectangles. Hence, I performed morphological transformations to close the contours, producing this:
And after edge extracting:
Leaving me with these "rectangles" with twisted corners in the edges. How can I approximate these pseudo-rectangles to perfect geometric rectangles?
Is there a more efficient way of doing this?
Here is my code so far:
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
using namespace cv;
using namespace std;
#define BKG_THRESH 60 // preProcessImg
Mat src;
void preProcessImg(Mat* _img){
Mat aux_gray;
cvtColor(*_img, aux_gray, CV_BGR2GRAY );
GaussianBlur(aux_gray, *_img, Size(5,5), 0);
}
int main( int argc, char** argv ){
vector<vector<Point>> contours;
/// Load an image
src = imread("img.jpg");
preProcessImg(&src);
Canny(src, src, 30, 200);
//Mostrar imagem
namedWindow( "canny_output", CV_WINDOW_NORMAL); // Create a window
imshow( "canny_output", src);
waitKey(0);
Mat structuringElement = getStructuringElement(MORPH_ELLIPSE, Size(7, 7));
morphologyEx(src, src, MORPH_CLOSE, structuringElement);
//Mostrar imagem
namedWindow( "morph_transf", CV_WINDOW_NORMAL); // Create a window
imshow( "morph_transf", src);
waitKey(0);
findContours(src, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++){
Scalar color( rand()&255, rand()&255, rand()&255 );
drawContours( drawing, contours, i, color );
}
//Mostrar imagem
namedWindow( "contours", CV_WINDOW_NORMAL); // Create a window
imshow( "contours", drawing);
waitKey(0);
return 0;
}
The more robust way is to find lines (Hough lines) afer Canny, intersect they and find rectangles. Contours are not robust for noise.

How can I get the color histogram inside the circle in the image or the gradient distribution in the circle?

I have detected circular objects in the image with OpenCV and C++ and using houghcircle function. But I would like to filter out only those circle that have dark color (it should be the airplane door window). So I thought to use the color histogram inside the detected circle in the image or the gradient distribution in those circle to filter out .It works on some images like in the image provided. But when there are more circles in the image does not find the dark circle. Also I played with canny threshold but I would like it do happened automatically. So basically how to make the program more robust?
Firstly here is the code in C++ using OpenCV and C++
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
#include <vector>
int thresh = 200;
int max_thresh = 400;
Mat src;
void thresh_callback(int, void* );
int main()
{
cv::Mat src = cv::imread("d4.png");
resize(src, src, Size(640,480), 0, 0, INTER_CUBIC);
char* source_window = "Source";
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src );
createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
thresh_callback( 0, 0 );
waitKey(0);
return(0);
}
void thresh_callback(int, void* ) {
Mat src_gray;
cv::Mat bgr_image = cv::imread( "d4.png");
cv::Mat orig_image = bgr_image.clone();
cvtColor( bgr_image, src_gray, COLOR_BGR2HSV );
medianBlur(src_gray, src_gray, 3);
Mat canny_output;
Canny( src_gray, canny_output, thresh, thresh*3.5, 3 );
// Threshold the HSV image, keep only the black pixels
cv::Mat lower_black_hue_range;
cv::Mat upper_black_hue_range;
cv::inRange(canny_output, cv::Scalar(0, 0, 0), cv::Scalar(10, 10, 40), lower_black_hue_range);
cv::inRange(canny_output, cv::Scalar(0,0, 41), cv::Scalar(10, 15, 50), upper_black_hue_range);
// Combine the above two images
cv::Mat black_hue_image;
cv::addWeighted(lower_black_hue_range, 1.0, upper_black_hue_range, 1.0, 0.0, black_hue_image);
cv::GaussianBlur(black_hue_image, black_hue_image, cv::Size(9, 9), 2, 2);
// Use the Hough transform to detect circles in the combined threshold image
std::vector<cv::Vec3f> circles;
cv::HoughCircles(black_hue_image, circles, CV_HOUGH_GRADIENT, 1, black_hue_image.rows/1, 10, 100, 10, 0);
// Loop over all detected circles and outline them on the original image
if(circles.size() == 0) std::exit(-1);
for(size_t current_circle = 0; current_circle < circles.size(); ++current_circle) {
Point center(cvRound(circles[current_circle][0]), cvRound(circles[current_circle][1]));
int radius = cvRound(circles[current_circle][2]);
cv::circle(orig_image, center, radius, cv::Scalar(0, 255, 0), 5);
}
// Show images
resize(lower_black_hue_range, lower_black_hue_range, Size(640,480), 0, 0, INTER_CUBIC);
char* source_window1 = "Threshold lower image";
namedWindow( source_window1, CV_WINDOW_AUTOSIZE );
imshow( source_window1, lower_black_hue_range );
//cv::namedWindow("Threshold lower image", cv::INTER_CUBIC);
//cv::imshow("Threshold lower image", lower_black_hue_range);
resize(upper_black_hue_range, upper_black_hue_range, Size(640,480), 0, 0, INTER_CUBIC);
char* source_window2 = "Threshold upper image";
namedWindow( source_window2, CV_WINDOW_AUTOSIZE );
imshow( source_window2, lower_black_hue_range );
//cv::namedWindow("Threshold upper image", cv::INTER_CUBIC);
//cv::imshow("Threshold upper image", upper_black_hue_range);
resize(black_hue_image, black_hue_image, Size(640,480), 0, 0, INTER_CUBIC);
char* source_window3 = "Combined threshold images";
namedWindow( source_window3, CV_WINDOW_AUTOSIZE );
imshow( source_window3, black_hue_image );
//cv::namedWindow("Combined threshold images", cv::INTER_CUBIC);
//cv::imshow("Combined threshold images", black_hue_image);
resize(orig_image, orig_image, Size(640,480), 0, 0, INTER_CUBIC);
char* source_window4 = "Detected black circles on the input image";
namedWindow( source_window4, CV_WINDOW_AUTOSIZE );
imshow( source_window4, orig_image );
//cv::namedWindow("Detected black circles on the input image", cv::INTER_CUBIC);
//cv::imshow("Detected black circles on the input image", orig_image);
}
Input image of good detected circle
The output is correct
Second image with more circles
The output is wrong, wrong detected circle
Any help?

Open CV Haarcascades not working properly

I've written a code for face and eye detection that worked fine previously but now due to unknwon reasons, the program is having problems and a message is displayed that Your Project has stopped working... Please have a look at the following code and the commented line number has the problem and the program stops there
#include <opencv2/core/core.hpp>
#include "opencv2/objdetect/objdetect.hpp"
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
/*Mat image=imread("im.jpg");
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
//imwrite("im2.jpg",image);// write the image stored in object image as im2.jpg
waitKey(0); // Wait for a keystroke in the window
return 0;*/
/*VideoCapture capture;
Mat frame;
capture.open(0);
if(capture.isOpened())
{
cout<<"success"<<endl;
while(1)
{
bool flag=capture.read(frame);
namedWindow( "Display window", WINDOW_AUTOSIZE );
if(flag)
imshow( "Display window", frame );
else
cout<<"failed";
waitKey(300);
}
}*/
//face detection
cout<<"i'm here";
CascadeClassifier face_cascade("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
CascadeClassifier eye_cascade("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml");
Mat image=imread("subject3.jpg");
cout<<"i'm here";
Mat res;
resize(image, res, Size(800, 600), 0, 0, INTER_LINEAR);
Mat gray;
cvtColor(res,gray,COLOR_BGR2GRAY);
equalizeHist(gray, gray);
std::vector<Rect> faces;
std::vector<Rect> eyes;
face_cascade.detectMultiScale(gray,faces, 1.1, 2,0 | CASCADE_SCALE_IMAGE, Size(30, 30));//problem
Rect roi;
cout<<faces.size();
Mat crop;
Mat grayEye;
for(int i=0;i<faces.size();i++)
{
cout<<"flag2";
roi.x=faces[i].x;
roi.y=faces[i].y;
roi.width=faces[i].width;
roi.height=faces[i].height;
Point pt1(faces[i].x, faces[i].y);
Point pt2((faces[i].x + faces[i].height), (faces[i].y + faces[i].width));
crop=res(roi);
resize(crop, crop, Size(300, 300), 0, 0, INTER_LINEAR);
cvtColor(crop, grayEye, CV_BGR2GRAY);
equalizeHist(grayEye, grayEye);
eye_cascade.detectMultiScale(grayEye,eyes, 1.1, 2,0 | CASCADE_SCALE_IMAGE, Size(30, 30));
cout<<endl<<eyes.size();
for(int j=0;j<eyes.size();j++)
{
Point pnt1(eyes[j].x, eyes[j].y);
Point pnt2((eyes[j].x + eyes[j].height), (eyes[j].y + eyes[j].width));
rectangle(crop, pnt1, pnt2, Scalar(0, 255, 0), 2);
}
rectangle(res, pt1, pt2, Scalar(0, 255, 0), 2);
}
namedWindow( "detected", WINDOW_AUTOSIZE );
imshow("detected",res);
waitKey(300);
imshow("detected",crop);
waitKey(0);
}
The following modifications made the program to work correctly (strange)
Previous Code :
CascadeClassifier face_cascade("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
Replaced with :
CascadeClassifier face_cascade;
face_cascade.load("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
I was loading the xml file in constructor previously (still don't know why it happened) but it is working now.