I've switched from Ubuntu to Windows for my opencv project and while displaying an image using the imshow function the image is displayed but the other details like x axis and y axis information and the intensity values were not shown in the window.
The same code under the Ubuntu build works perfectly. Here is my code:
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
int main()
{
cv::Mat imgrgb = imread("C:\\Users\\Len\\Documents\\project\\Images\\1-11.jpg", CV_LOAD_IMAGE_COLOR);
// Check that the image read is a 3 channels image and not empty
CV_Assert(imgrgb.channels() == 3);
if (imgrgb.empty()) {
cout << "Image is empty. Specify correct path" << endl;
return -1;
}
cv::cvtColor(imgrgb, img, CV_BGR2GRAY);
namedWindow("Test", cv::WINDOW_AUTOSIZE);
imshow("Test", imgrgb);
waitKey(0);
}
So, how can I display the intensity values along with the current x and y axis information?
Related
My question is about trying to fixing the italized line so that my overlay will work properly and instead of black pixels there are white pixels based on my conditional statement. I have tried several things such as using different types such as:
out1.at<Vec3b>(i,j)[0]=image.at<Vec3b>(i,j)[0];
out1.at<Vec3b>(i,j)[1]=image.at<Vec3b>(i,j)[1];
out1.at<Vec3b>(i,j)[2]=image.at<Vec3b>(i,j)[2];
But I got a heap error. I believe I am really close but I need some advice or guidance. Please excuse any errors that I have made posting for this is my first post.
Here is my code.
#include <iostream>
#include <stdint.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
using namespace std;
using namespace cv;
int main(int argv, char** argc)
{
Mat image; // new blank image
Mat image2;
Mat out1;
image2 = cv::imread("test2.bmp",CV_LOAD_IMAGE_GRAYSCALE); // read the file
image = cv::imread("test1.bmp",CV_LOAD_IMAGE_GRAYSCALE);
if (!image.data) // error handling if file does not load
{
cout<< "Image 1 not loaded";
return -1;
}
if (!image2.data)
{
cout << "Image 2 not loaded";
return -1;
}
// resize images to make sure all images is the same size
cv::resize(image2, image2, image.size());
cv::resize(image2, out1, image.size());
// copying content of overlay image to output file
//image2.copyTo(out1);
out1 = image2.clone();
// for loop comparing pixels to original image
for (int i =0; i < out1.rows; i++)
{
for(int j =0; j < out1.cols; j++)
{
//Vec3b color = image.at<Vec3b>(Point(i,j));
if(out1.at<uchar>(i,j)==0 && out1.at<uchar>(i,j) ==0 &&
out1.at<uchar>(i,j)==0)
{
out1.at<Vec3b>(i,j)[0]=255; // blue channel
out1.at<Vec3b>(i,j)[1]=255; // green channel
out1.at<Vec3b>(i,j)[2]=255; // red channel
}
else
*out1.at<uchar>(i,j) = image.at<uchar>(i,j);*
}
}
cv::imwrite("out1.bmp",out1); // save to output file
namedWindow("Display window", CV_WINDOW_AUTOSIZE);// creat a window to
display w/label
imshow("Display window",out1); // show image inside display window
waitKey(0);
return 0;
}
My image is close to being overlayed correctly. My issue is that the
pixels shows up black instead of white due to a certain line in my
program
I am trying to code a simple C++ routine to first write a predefined dictionary of ArUco markers (e.g. 4x4_100) to a folder and then detect ArUco markers in a specific image selected from the folder using OpenCV 3.1 and Visual Studio 2017. I have compiled all the OpenCV-contrib libraries required to use ArUco markers. My routine builds without any error, but I am having trouble detecting the markers even after supplying all the correct arguments (e.g. image, Dictionary, etc.) to the in-built "aruco::detectMarkers" function. Could you please help me understand what`s wrong with my approach? Below is a minimal working example and the test image is attached here "4x4Marker_40.jpg":
#include "opencv2\core.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2\imgcodecs.hpp"
#include "opencv2\aruco.hpp"
#include "opencv2\highgui.hpp"
#include <sstream>
#include <fstream>
#include <iostream>
using namespace cv;
using namespace std;
// Function to write ArUco markers
void createArucoMarkers()
{
// Define variable to store the output markers
Mat outputMarker;
// Choose a predefined Dictionary of markers
Ptr< aruco::Dictionary> markerDictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME::DICT_4X4_50);
// Write each of the markers to a '.jpg' image file
for (int i = 0; i < 50; i++)
{
aruco::drawMarker(markerDictionary, i, 500, outputMarker, 1);
ostringstream convert;
string imageName = "4x4Marker_";
convert << imageName << i << ".jpg";
imwrite(convert.str(), outputMarker);
}
}
// Main body of the routine
int main(int argv, char** argc)
{
createArucoMarkers();
// Read a specific image
Mat frame = imread("4x4Marker_40.jpg", CV_LOAD_IMAGE_UNCHANGED);
// Define variables to store the output of marker detection
vector<int> markerIds;
vector<vector<Point2f>> markerCorners, rejectedCandidates;
// Define a Dictionary type variable for marker detection
Ptr<aruco::Dictionary> markerDictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME::DICT_4X4_50);
// Detect markers
aruco::detectMarkers(frame, markerDictionary, markerCorners, markerIds);
// Display the image
namedWindow("Webcam", CV_WINDOW_AUTOSIZE);
imshow("Webcam", frame);
// Draw detected markers on the displayed image
aruco::drawDetectedMarkers(frame, markerCorners, markerIds);
cout << "\nmarker ID is:\t"<<markerIds.size();
waitKey();
}
There are a few problems in your code:
You are displaying the image with imshow before calling drawDetectedMarkers so you'll never see the detected marker.
You are displaying the size of the markerIds vector instead of the value contained within it.
(This is the main problem) Your marker has no white space around it so it's impossible to detect.
One suggestion: use forward slashes, not backslashes in your #include statements. Forward slashes work everywhere, backslashes only work on Windows.
This worked on my machine. Note that I loaded the image as a color image to make it easier to see the results of drawDetectedMarkers.
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/aruco.hpp>
#include <opencv2/highgui.hpp>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace cv;
using namespace std;
// Function to write ArUco markers
void createArucoMarkers()
{
// Create image to hold the marker plus surrounding white space
Mat outputImage(700, 700, CV_8UC1);
// Fill the image with white
outputImage = Scalar(255);
// Define an ROI to write the marker into
Rect markerRect(100, 100, 500, 500);
Mat outputMarker(outputImage, markerRect);
// Choose a predefined Dictionary of markers
Ptr< aruco::Dictionary> markerDictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME::DICT_4X4_50);
// Write each of the markers to a '.jpg' image file
for (int i = 0; i < 50; i++)
{
//Draw the marker into the ROI
aruco::drawMarker(markerDictionary, i, 500, outputMarker, 1);
ostringstream convert;
string imageName = "4x4Marker_";
convert << imageName << i << ".jpg";
// Note we are writing outputImage, not outputMarker
imwrite(convert.str(), outputImage);
}
}
// Main body of the routine
int main(int argv, char** argc)
{
createArucoMarkers();
// Read a specific image
Mat frame = imread("4x4Marker_40.jpg", CV_LOAD_IMAGE_COLOR);
// Define variables to store the output of marker detection
vector<int> markerIds;
vector<vector<Point2f>> markerCorners, rejectedCandidates;
// Define a Dictionary type variable for marker detection
Ptr<aruco::Dictionary> markerDictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME::DICT_4X4_50);
// Detect markers
aruco::detectMarkers(frame, markerDictionary, markerCorners, markerIds);
// Display the image
namedWindow("Webcam", CV_WINDOW_AUTOSIZE);
// Draw detected markers on the displayed image
aruco::drawDetectedMarkers(frame, markerCorners, markerIds);
// Show the image with the detected marker
imshow("Webcam", frame);
// If a marker was identified, show its ID
if (markerIds.size() > 0) {
cout << "\nmarker ID is:\t" << markerIds[0] << endl;
}
waitKey(0);
}
Please can someone here who can help me with this. I'm trying to rotate a segmented region of an image to align the major axis horizontally.
I have a segmented region in the center of the image following the steps used herein. Move area of an image to the center using OpenCV
I read this OPENCV: PCA application error in image_proc, but it does not help me solve my problem.
I have this
I want this
Slightly different than how Miki suggested, I used findNonZero, minAreaRect, and WarpAffine.
You can either use 270 or 90 on the getRotationMatrix2D to align the major axis with the horizontal.
#include "stdafx.h"
#include <opencv/cxcore.h>
#include <opencv2\core\mat.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/videoio/videoio.hpp>
using namespace cv;
using namespace std;
int main() {
//getting the image
Mat image = imread("C:/this/is/a/path/to/an/image.png");
//create new image that looks exactly like old image
Mat rot_image = image.clone();
rot_image = Scalar(0);
//showing the image
namedWindow("Image", CV_WINDOW_NORMAL| CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED);
namedWindow("Rotated Image", CV_WINDOW_NORMAL| CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED);
imshow("Image", image);
waitKey(0);
imshow("Rotated Image", rot_image);
waitKey(0);
//convert image
Mat img_bw;
inRange(image, Scalar(1,1,1), Scalar(255,255,255), img_bw);
imshow("Rotated Image", img_bw);
waitKey(0);
//find coordinates
Mat nonZeroCoordinates;
findNonZero(img_bw, nonZeroCoordinates);
RotatedRect rect = minAreaRect(nonZeroCoordinates);
rect.center = Point(image.cols/2, image.rows/2);
//get the Rotation Matrix
Mat M = getRotationMatrix2D(rect.center, 270, 1.0);
// perform the affine transformation
warpAffine(image, rot_image, M, image.size(), INTER_CUBIC);
//displaying the image
imshow("Rotated Image", rot_image);
waitKey(0);
//saving the new image
imwrite("C:/this/is/a/path/to/a/rotatedImage.png", rot_image);
}
That code turns this:
to this:
You can take the rect.center line out if you're sure your object is already going to be in the center.
I am trying to convert color images to gray-scale using OpenCV 2.4.11 C++ for Visual Studio 2012. I have used the following code to convert the image to grayscale. However, I am unable to do so because I am not able to read the image.
The message I get is "Error reading image" because img is empty. I have stored the required image in the Debug folder beside the exe file. I have also mentioned the image name as a command argument in the Debug section of the property pages. I am also trying to store the grayscale image in the disk. Thanks in advance. The code is as follows:
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
using namespace cv;
int main(int argc, const char**argv)
{
Mat img=imread("Mountain_8-Bit_Grayscale.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if(img.empty())
{
std::cout<< " --(!) Error reading image " << std::endl;
system("pause");
return -2;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
imwrite("image_bw.jpg", img);
waitKey(0);
destroyWindow("MyWindow");
return 0;
}
You should try using the absolute path to the image not the relative path. The other steps are fine, the image is read properly and the image displaying and saving commands are given properly there is a path problem.
I'm pretty new to OpenCV, so bear with me. I'm running a Mac Mini with OSX 10.8. I have a program that recognizes colors and displays them in binary picture (black and white). However, I want to store the number of white pixels as an integer (or float, etc.) to compare with other number of pixels. How can I do this? Here is my current code-
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
VideoCapture cap(0); //capture the video from webcam
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
namedWindow("HSVLeftRed", CV_WINDOW_AUTOSIZE);
namedWindow("HSVLeftGreen", CV_WINDOW_AUTOSIZE);
while (true) {
Mat image;
cap.read(image);
Mat HSV;
Mat leftgreen;
Mat leftred;
//Left Cropping
Mat leftimg = image(Rect(0, 0, 640, 720));
//Left Red Detection
cvtColor(leftimg,HSV,CV_BGR2HSV);
inRange(HSV,Scalar(0,0,150),Scalar(0,0,255), leftgreen);
//imshow("HSVLeftRed", leftgreen);
//print pixel type
//Left Green Detection
cvtColor(leftimg,HSV,CV_BGR2HSV);
inRange(HSV,Scalar(still need to find proper min values),Scalar(still need to find proper max values), leftgreen);
//imshow("HSVLeftGreen", leftgreen);
//compare pixel types
}
return 0;
}
Thanks in advance!
To count the non-zero pixels, OpenCV has this function cv::countNonZero. It takes input the image, whose number of non-zero pixels, we want to calculate and output is number of non-zero pixels(int). Here is the documentation.
In your case, since all the pixels are either black or white, all the non zero pixels will be white pixels.
This is how to use it,
int cal = countNonZero(image);
Change image, as per your code.