I want to display the image on any computer that opens the program .. But the software shows the picture only that the image and the software are in the same place (I want the image to be inside the software)
And if it is not in the same place then it shows me this error: (image)
https://i.imgur.com/bEtdaif.png
#include <iostream>
#include <Windows.h>
#include <opencv2/opencv.hpp>
#include "opencv2\highgui.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("d.png");
namedWindow("Image");
imshow("Image", img);
waitKey(0);
cout << "h";
int i;
cin >> i;
}
One way is to write a program that converts the image to a string of the form std::vector<uint8_t> image{ 0x01, 0x02 ... }; list each byte. Then save that string in a file.
Then #include that file into your program and read the image data from the image variable.
This way the image will be embedded in your executable by the compiler.
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
Mat src = imread("image.png", 1);
namedWindow("src", 1);
imshow("src", src);
vector<Mat> rgbChannels(3);
split(src, rgbChannels);
namedWindow("R", 1);
imshow("R", rgbChannels[2]);
waitKey(0);
return 0;
}
.
I was expecting something like the following:
why doesn't the above code show the Red channel? why does it show a grayscale image?
if the image is split into 3 channels, each matrix should show one of the colors of r, g, and b. isn't that so?
Your code is correct; however, OpenCV is showing the channel as grayscale. Mat does not keep the information about "where" the data came from. In other words, it does not know it was a red channel, so when you call imshow, it displays it as a single-channel image.
What you can do is build up an empty image with 2 zero'd channels and the one you want to visualize.
So Im trying to write a single character using putText() on top of an image to fit into a 25x25 box but the text is too small to render, it just looks like a blob of whatever color I choose the text to be. Is there any way to create small, readable text to overlay onto an image with OpenCV?
Here is an example using both putText() and also loading a character from a file created in Photoshop or GIMP.
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>
using namespace cv;
using namespace std;
int
main(int argc,char*argv[])
{
// Make a 3 channel image
cv::Mat main(200,300,CV_8UC3);
// Fill entire image with magenta
main = cv::Scalar(255,0,255);
// Load a character "M" from a file and overlay
Mat txt = cv::imread("M.png",-CV_LOAD_IMAGE_ANYDEPTH);
txt.copyTo(main(cv::Rect(80,120,txt.cols,txt.rows)));
// Now use puttext() to do a white S
int fontFace = FONT_HERSHEY_COMPLEX_SMALL;
double fontScale=1.5;
string text="S";
putText(main,"S",Point(60,100),fontFace,fontScale,Scalar(255,255,255));
// Save to disk
imwrite("result.png",main);
}
Here's the M.png file:
Here's the result:
I also notice that the anti-aliased fonts (on the right side in image below) look somewhat easier to read:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>
using namespace cv;
using namespace std;
int
main(int argc,char*argv[])
{
// Make a 3 channel image
cv::Mat main(280,800,CV_8UC3);
// Fill entire image with magenta
main = cv::Scalar(255,0,255);
double fontScale=1.5;
int thickness=1;
int x=10,y=40;
putText(main,"Simplex",Point(x,y),CV_FONT_HERSHEY_SIMPLEX,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Simplex AA",Point(x+400,y),CV_FONT_HERSHEY_SIMPLEX,fontScale,Scalar(255,255,255),thickness,CV_AA);
y+=40;
putText(main,"Plain",Point(x,y),CV_FONT_HERSHEY_PLAIN,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Plain AA",Point(x+400,y),CV_FONT_HERSHEY_PLAIN,fontScale,Scalar(255,255,255),thickness,CV_AA);
y+=40;
putText(main,"Duplex",Point(x,y),CV_FONT_HERSHEY_DUPLEX,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Duplex AA",Point(x+400,y),CV_FONT_HERSHEY_DUPLEX,fontScale,Scalar(255,255,255),thickness,CV_AA);
y+=40;
putText(main,"Complex",Point(x,y),CV_FONT_HERSHEY_COMPLEX,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Complex AA",Point(x+400,y),CV_FONT_HERSHEY_COMPLEX,fontScale,Scalar(255,255,255),thickness,CV_AA);
y+=40;
putText(main,"Triplex",Point(x,y),CV_FONT_HERSHEY_TRIPLEX,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Triplex AA",Point(x+400,y),CV_FONT_HERSHEY_TRIPLEX,fontScale,Scalar(255,255,255),thickness,CV_AA);
y+=40;
putText(main,"Script",Point(x,y),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,fontScale,Scalar(255,255,255),thickness,8);
putText(main,"Script AA",Point(x+400,y),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,fontScale,Scalar(255,255,255),thickness,CV_AA);
// Save to disk
imwrite("result.png",main);
}
I have this code which captures image from Webcam using OpenCV:
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main( )
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat meter_image;
cap >> meter_image;
imwrite("/boneCV-master/img.jpg", meter_image);
return 0;
}
I get following image as output.
Previously it was working fine. I don't know what is happening. I tried simplest of all codes upon googling but nothing worked. Please let me know what could be wrong with it.
thanks in advance.
EDIT
One thing I forgot to mention is that i am working on beagleBone Black. this same piece of codes works great with my mac.
Maybe adding frame checking will be helpful.
Mat meter_image;
while(meter_image.empty()){
cap >> meter_image;
}
But there's a risk of infinite loop.
I'm trying to test some motion estimation in Visual Studio 2013, using OpenCV v3.0 (which is probably my 1st mistake!). I got an unhandled exception trying to use createOptFlow_DualTVL1() and createOptFlow_Farneback(), and then, for testing, tried cv::accumulate(), which threw the same exception.
It seems that OpenCV can't write to the Mat object that I'm passing these functions. I can't read the actual cvException because I don't have the PDB files, because I didn't compile this version myself. That might be my next stop, but before I do I figured I'd see if anyone's seen this behaviour before.
Here's a minimal working example:
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
#include <regex>
using namespace std;
using namespace cv;
int main(int argc, const char** argv)
{
VideoCapture captureDevice;
std::string videoname = "example.mp4";
captureDevice.open(videoname);
//setup image files used in the capture process
Mat currFrame, dst;
captureDevice >> currFrame;
accumulate(currFrame, dst, cv::noArray());
imshow("outputCapture", dst);
//pause for 33ms
waitKey(33);
return 0;
}
dst should be of same size as that currFrame and of type CV_32FC3.
So, add this line of code before calling accumulate -
dst.create(currFrame.size(), CV_32FC3);
Since dst is of float type, you will need to convert it to uchar to display it. For that, convert as shown below -
Mat dst_disp;
dst.convertTo(dst_disp, CV_8UC3);
imshow("outputCapture",dst_disp );
Additionally, as you accumulate more frames in dst, you will need to normalize by number of frames(let's say N) cached in dst. Simply, divide dst by the N, then convert the result into CV_8UC3 and display. For example, if you accumulated 1000 frames in dst do as shown below,
// Accumulate 1000 frames
for(int i = 0; i < 1000; i++)
accumulate(currFrame, dst, cv::noArray());
// Normalize
dst = dst/ 1000;
// Display the frame
Mat dst_disp;
dst.convertTo(dst_disp, CV_8UC3);
imshow("outputCapture",dst_disp );
else, you might get an all white image.
UPDATE
From #berak's comment below.
For normalization, simply use
dst.convert(dst_disp, CV_8UC3, 1.0/N);
where N in example above will be 1000.