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.
Related
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);
For example, I create
Mat mat1 = Mat::zeros(Size(100, 100), CV_8UC3);
and fill each pixel with (0, 255, 255), which is supposed to be red in hsv.
However, if I imshow this mat, this will be printed as a BGR image and is not red.
How do I make this mat hsv format and setting (0, 255, 255) result in red?
imshow assumes that the image you pass to it is in BGR color space. However, you can create a small function that does your imshow of HSV images.
void imshowHSV(std::string& name, cv::Mat& image)
{
cv::Mat hsv;
cv:cvtColor(image, hsv, CV_HSV2BGR);
cv::imshow(name, hsv);
}
But beware! this will convert and create a copy of the image, if you over use it it may have quite some overhead :)
I write a simple app in OpenCV that delete black background of an image and save it with white background in JPG. However, it's always saved with black background.
This is my code:
Mat Imgsrc = imread("../temp/temp1.jpg",1) ;
mat dest;
Mat temp, thr;
cvtColor(Imgsrc, temp, COLOR_BGR2GRAY);
threshold(temp,thr, 0, 255, THRESH_BINARY);
Mat rgb[3];
split(Imgsrc,rgb);
Mat rgba[4] = { rgb[0],rgb[1],rgb[2],thr };
merge(rgba,4,dest);
imwrite("../temp/r5.jpg", dest);
You can simply use setTo with a mask to set some pixels to a specific value according to a mask:
Mat src = imread("../temp/temp1.jpg",1) ;
Mat dst;
Mat gray, thr;
cvtColor(src, gray, COLOR_BGR2GRAY);
// Are you sure to use 0 as threshold value?
threshold(gray, thr, 0, 255, THRESH_BINARY);
// Clone src into dst
dst = src.clone();
// Set to white all pixels that are not zero in the mask
dst.setTo(Scalar(255,255,255) /*white*/, thr);
imwrite("../temp/r5.jpg", dst);
Also a few notes:
You can directly load an image as grayscale using: imread(..., IMREAD_GRAYSCALE);
You can avoid to use all those temporary Mats.
Are you sure you want to use 0 as threshold value? Because in this case you can avoid entirely to apply theshold, and set to white all pixels that are 0 in the grayscale image: dst.setTo(Scalar(255,255,255), gray == 0);
This is how I'd do:
// Load the image
Mat src = imread("path/to/img", IMREAD_COLOR);
// Convert to grayscale
Mat gray;
cvtColor(src, gray, COLOR_BGR2GRAY);
// Set to white all pixels that are 0 in the grayscale image
src.setTo(Scalar(255,255,255), gray == 0)
// Save
imwrite("path/to/other/img", src);
When I convert a QImage to cv::Mat the Mat comes out blurred.
This is the code that converts it:
QPixmap pixmap(*ui->imgLabel->pixmap());
QImage image = pixmap.toImage();
image.convertToFormat(QImage::Format_RGB888);
Mat matImage = Mat(image.height(), image.width(), CV_8UC3, image.scanLine(0));
This is the original image zoomed:
This is the image created:
Grateful for any help.
I don't know QT, but have you tried ready solutions from WEB? For example, from this link I found another method of conversion:
Mat qimage2mat(const QImage& qimage) {
cv::Mat mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC4, (uchar*)qimage.bits(), qimage.bytesPerLine());
cv::Mat mat2 = cv::Mat(mat.rows, mat.cols, CV_8UC3 );
int from_to[] = { 0,0, 1,1, 2,2 };
cv::mixChannels( &mat, 1, &mat2, 1, from_to, 3 );
return mat2;
};
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);.