Add text labels in opencv image - c++

I have got an image in opencv, and I want to add labels in the axis. Actually I want similar implementation as xlabel and ylabel matlab functions. How is it then, to add labels in both x and y axis of an image?

If you have a string a label variable label image matrix img and positions x and y , you can do
putText(img, label, Point(x, y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
The additional parameters are for font attributes.

You will need to get the size of the original image and then create a new image that is larger than the existing image from this, manually draw your Axis in this image, and label them using the putText function. Then insert the original image into the relevant portion of this new image.
Images dont have "Axis" as such, so your labels and axis will need to be part of the image!

Related

How can I tile an image within a given area of a page in my document, using the Mako SDK?

I'm looking to use an image for tilling, to fill and area of my page, in my document.
I've seen that there is an IDOMImage and IDOMImageBrush, but I'm not sure how to use them in order to scale and tile my source image.
How can I do this with the Mako SDK?
Mako can tile an image into a given area, and also flip alternate tiles to create a pattern. Use a scaling transform to control its size. This code shows you how.
// Declare an output pointer
IOutputPtr output;
// Create new assembly, document and page
IDocumentAssemblyPtr assembly = IDocumentAssembly::create(jawsMako);
IDocumentPtr document = IDocument::create(jawsMako);
IPagePtr page = IPage::create(jawsMako);
// Add the page to the document, and the document to the assembly
document->appendPage(page);
assembly->appendDocument(document);
// Create a fixed page to work with
double pageWidth = 10 * 96.0;
double pageHeight = 20 * 96.0;
IDOMFixedPagePtr fixedPage = IDOMFixedPage::create(jawsMako, pageWidth, pageHeight);
// Load the image file into an image
IDOMImagePtr image = IDOMJPEGImage::create(jawsMako, IInputStream::createFromFile(jawsMako, imageFilePath));
// Find its dimensions
IImageFramePtr frame;
image->getFirstImageFrame(jawsMako, frame);
double imageWidth = frame->getWidth();
double imageHeight = frame->getHeight();
// Create a rect to hold the image
FRect printBounds(0.0, 0.0, pageWidth, pageHeight);
// Create a transformation matrix to scale the image, taking into account the page proportions
// Scaling factor is a float ranging from 0.0 to 1.0
double pageWidthHeightRatio = pageWidth / pageHeight;
FMatrix transform;
transform.scale(scalingFactor, scalingFactor * pageWidthHeightRatio);
// Stick the image in a brush
IDOMBrushPtr imageBrush = IDOMImageBrush::create(jawsMako, image, FRect(), printBounds, transform, 1.0, eFlipXY);
// And now create a path using the image brush
IDOMPathNodePtr path = IDOMPathNode::createFilled(jawsMako, IDOMPathGeometry::create(jawsMako, printBounds), imageBrush);
// Add the path to the fixed page
fixedPage->appendChild(path);
// This becomes the page contents
page->setContent(fixedPage);
// Write to the output
output = IPDFOutput::create(jawsMako);
output->writeAssembly(assembly, outputFilePath);
Using this code, with this image:
Produced this tilled image:
The code uses an enum, eTileXY. These are the available tiling options:
eTilingMode
Tiling mode type enumeration.
eNoTile
No tiling. If the area to be painted is larger than the image, just paint the image once (in the location specified by the brush's viewport), and leave the remaining area transparent.
eTile
Tile image without any flipping or rotating of the image. A square image consisting of a single diagonal line between opposite corners would produce diagonal lines when tiled in this mode.
eFlipX
Tile image such that alternate columns of tiles are flipped horizontally. A square image consisting of a single diagonal line between opposite corners would produce chevrons running horizontally across the area when tiled in this mode.
eFlipY
Tile image such that alternate rows of tiles are flipped vertically. A square image consisting of a single diagonal line between opposite corners would produce chevrons running vertically across the area when tiled in this mode.
eFlipXY
Tile image such that alternate columns of tiles are flipped horizontally AND alternate rows of tiles are flipped vertically. A square image consisting of a single diagonal line between opposite corners would produce a grid of squares balanced on their points when tiled in this mode.

VTK - how to flip\mirror image

I'm using vtkResliceImageViewer to display image (multi-planar reconstruction). How can I flip\mirror that image vertically and horizontally? Operating with camera is not working as expected, since flip has to take into consideration also camera rotation angle, so it gets very complicated. It would be great if there is a way to change image's texture coordinates. Is this possible?
// Create an image
vtkSmartPointer<vtkImageMandelbrotSource> source =
vtkSmartPointer<vtkImageMandelbrotSource>::New();
source->Update();
// Flip the image
vtkSmartPointer<vtkImageFlip> flipYFilter =
vtkSmartPointer<vtkImageFlip>::New();
flipYFilter->SetFilteredAxis(1); // flip y axis
flipYFilter->SetInputConnection(source->GetOutputPort());
flipYFilter->Update();
// Create the Viewer
vtkSmartPointer<vtkResliceImageViewer> viewer =
vtkSmartPointer<vtkResliceImageViewer>::New();
viewer->SetInputData(flipYFilter->GetOutput())

OpenCV Binary Image Mask for Image Analysis in C++

I'm trying to analyse some images which have a lot of noise around the outside of the image, but a clear circular centre with a shape inside. The centre is the part I'm interested in, but the outside noise is affecting my binary thresholding of the image.
To ignore the noise, I'm trying to set up a circular mask of known centre position and radius whereby all pixels outside this circle are changed to black. I figure that everything inside the circle will now be easy to analyse with binary thresholding.
I'm just wondering if someone might be able to point me in the right direction for this sort of problem please? I've had a look at this solution: How to black out everything outside a circle in Open CV but some of my constraints are different and I'm confused by the method in which source images are loaded.
Thank you in advance!
//First load your source image, here load as gray scale
cv::Mat srcImage = cv::imread("sourceImage.jpg", CV_LOAD_IMAGE_GRAYSCALE);
//Then define your mask image
cv::Mat mask = cv::Mat::zeros(srcImage.size(), srcImage.type());
//Define your destination image
cv::Mat dstImage = cv::Mat::zeros(srcImage.size(), srcImage.type());
//I assume you want to draw the circle at the center of your image, with a radius of 50
cv::circle(mask, cv::Point(mask.cols/2, mask.rows/2), 50, cv::Scalar(255, 0, 0), -1, 8, 0);
//Now you can copy your source image to destination image with masking
srcImage.copyTo(dstImage, mask);
Then do your further processing on your dstImage. Assume this is your source image:
Then the above code gives you this as gray scale input:
And this is the binary mask you created:
And this is your final result after masking operation:
Since you are looking for a clear circular center with a shape inside, you could use Hough Transform to get that area- a careful selection of parameters will help you get this area perfectly.
A detailed tutorial is here:
http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html
For setting pixels outside a region black:
Create a mask image :
cv::Mat mask(img_src.size(),img_src.type());
Mark the points inside with white color :
cv::circle( mask, center, radius, cv::Scalar(255,255,255),-1, 8, 0 );
You can now use bitwise_AND and thus get an output image with only the pixels enclosed in mask.
cv::bitwise_and(mask,img_src,output);

OpenCV measure rectangular image size

I have an app that finds an object in a frame and uses warpPerspective to correct the image to be square. In the course of doing so you specify an output image size. However, I want to know how to do so without harming its apparent size. How can I unwarp the 4-corners of the image without changing the size of the image? I don't need the image itself, I just want to measure its height and width in pixels within the original image.
Get a transform matrix that will square up the corners.
std::vector<cv::Point2f> transformedPoints;
cv::Mat M = cv::getPerspectiveTransform(points, objectCorners);
cv::perspectiveTransform(points, transformedPoints, M);
This will square up the image, but in terms of the objectCorners coordinate system. Which is -0.5f to 0.5f not the original image plane.
BoundingRect almost does what I want.
cv::Rect boundingRectangle = cv::boundingRect(points);
But as the documentation states
The function calculates and returns the minimal up-right bounding rectangle for the specified point set.
And what I want is the bounding rectangle after it has been squared-up, not without squaring it up.
According to my understanding to your post, here is something which should help you.
OpenCV perspective transform example.
Update if it still doesn't help you out in finding the height and width within the image
Minimum bounding rect of the points
cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
As the minAreaRect reference on OpenCV's website states
Finds a rotated rectangle of the minimum area enclosing the input 2D point set.
You can call box.size and get the width and height.

openCV combining an image to another image on given coordinates

i wrote a face & eye detection code
next step is put an image to the coordinates of the detected eye (for ex: eye
patch, eye glasses)
i couldn't find the function to combine the source frame and the image I want to add
any suggestions
thanks
You can use cvCopy with a mask to do this. If the the images do not have the same height and width set the ROI of the destination image before using cvCopy.
See OpenCV documentation:
cvCopy
cvSetImageROI