How do I add a watermark to an image with OpenCV? - c++

Is there any way to add a watermark image to another image using the OpenCV library?
I am working with OpenCV and C++.

Take a look at this tutorial.
Here is some (non-tested) code:
Mat watermark = imread("watermarkimage.png")
Mat img = imread("yourimage.jpg");
//assuming the watermark has same number of channel and size as your_image
Mat watermarked = (0.8 * img) + (0.2 * watermark)

You can add text watermark on the image using cv::putText function of opencv.

Simple: blend the original image (90%) and the copy with the watermark text/graphics added (10%).

Related

Applying adaptive threshold to a grayscale image

I have a png image which is in grayscale 'test.png'. I need apply adaptive threshold to this image. I am using OpenCV.
image = cv2.imread('test_big.png')
im = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
I am not able to apply adaptive threshold since the image is not in grayscale.
So I tried to read the image as grayscale:
image = cv2.imread('test_big.png',1)
Now I can apply adaptive threshold but the output will be a blue and red image instead of black and white. Can anyone help?
The fault lies in the second code snippet:
image = cv2.imread('test_big.png',1)
Although you have said that test_big.png is a grayscale image, you have declared it as a color image (RGB) with three channels.
Hence you have to change the code to
image = cv2.imread('test_big.png', 0)
0 -> grayscale image
1 -> color image
You can also try:
cv2.imread('test_big.png', cv2.IMREAD_GRAYSCALE)
The bottom line is: although the image being read is a grayscale image, the system will not recognize it until it is explicitly specified. In your case, your image was a grayscale image, but since you declared it as a color image it considered the image to have three channels (RGB) and hence the subsequent adaptive threshold function did not execute.

Blurring non-rectangular region of an image, then downsample whole image - OpenCV

I am using OpenCV3 to blur and downsample an image, I just want to blur the region inside the area defined by a binary mask like:
After that region is blurred, I would like to downsample the image without blurring the whole image again, so pyrDown is not useful for me.
Any idea?
Tricky way:
cv::Mat roi;
cv::blur(image & mask,roi,cv::Size(3,3));//Or whatever blurring you want
cv::Mat Result=(image & (~mask)) + roi;
cv::resize(result,result,cv::Size(New_Width,New_height)); // Or whatever downsampling you want

converting color image to gray scale image

I am facing a problem with writing and reading a gray scale image in open cv using c++. This is my code to read a color image as gray scale image and to write it in a file:
cv::Mat source = cv::imread("c:\\users\\selva\\desktop\\newimage.jpg",CV_LOAD_IMAGE_GRAYSCALE);
cv::imwrite("c:\\users\\selva\\desktop\\grayscal.jpg",source);
Then i tried to read that image in a new project,
cv::Mat gray = cv::imread("c:\\users\\selva\\desktop\\grayscal.jpg",CV_LOAD_IMAGE_UNCHANGED);
But when i execute this code,
std::cout<<gray.channels(); The output in console is 3.
I am using opencv 2.4.7, i tried with .png format, but that does not help.
console value for std::cout<<source.channels(); is 1.
If my newimage.jpg is a gray scale image and i am reading as,
cv::Mat source = cv::imread("c:\\users\\selva\\desktop\\newimage.jpg",0);
the problem is same.
I am sure that i am loading correct image.
Please anyone answer why it happens. Thanks in advance.

OpenCV - JPEG compression on arm/android phone doesn't result the right image

I have written an android app which uses OpenCV to manipulate with images. I'm using the below code to write a cv::Mat object to JPG file.
cv::imwrite("<sd card path>/img.jpg", <some mat object>);
I do see the image being saved on my sd card, however, the colors are not right. It has some bluish color all over the image.
Does anyone know what I'm missing here?
The above comment by Haris resolved this issue. I modified the code to change the color space as below:
cv::Mat mat;
// Initialize mat
cv::cvtColor(mat, mat, CV_BGR2RGB);
cv::imwrite("<sd card path>/img.jpg", mat);
I was able to see the right image being saved with this.

How to determine a region of interest and then crop an image using OpenCV c++

I have a sample image as below. I would like to make the square my Region of Interest and then crop out that part (square) and create a new image with it. I will be working with different images so the square won't always be at the same location in all images. So I will need to somehow detect the edges of the square.
I can not post pictures because I have 10 reputations
help me please??
Thank you very much
cvSetImageROI(currentImage,yourSquare);
CvSize size = cvSize(width, height);
IplImage * newImage = cvCreateImage(size,8,3);
cvResize(currentImage,newImage,CV_INTER_AREA);
First you must set your ROI with cvSetImageROI(IplImage*,CvRect). After that you create your new image with the desired width and height. Finally you can copy your old image to the new one. The new image will contain your desired ROI.