OpenCV adding logo to a region of interest - opencv

I am trying to add a logo to a bigger image but I get the following error.
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file C:\opencv246\modules\core\src\matrix.cpp, line 323
terminate called after throwing an instance of ’cv::Exception’
what(): C:\opencv246\modules\core\src\matrix.cpp:323: error: (-215) 0 <= roi.X && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application’s support team for more information.
can someone please correct my code? I have been trying to fix it for three hours but still have not been able to do it.
here is my code.
using namespace std;
using namespace cv;
int main()
{
Mat image = imread("C:\\castle.jpg",0);
Mat logo = imread("C:\\logo.jpg",0);
Mat imageROI = image(cv::Rect(385,270,logo.cols,logo.rows));
addWeighted(imageROI,1.0,logo,0.3,0.,imageROI);
namedWindow("output",CV_WINDOW_AUTOSIZE);
imshow("output",imageROI);
waitKey(0);
destroyAllWindows();
}

Your image probably doesn't have the right size. What are the dimensions in pixels of image and logo? If image.cols < 385 + logo.cols or image.rows < 270 + logo.rows you will not be able to overlap this logo on this image at this position.

Related

OpenCV: Why am I getting assertion failed on original Mat but not on the cloned one?

I am trying to apply the Sobel filter to my Mat which has data from an external source. The simplified code looks like the following:
// Create Mats
cv::Mat src = cv::Mat( cv::Size( ACQ_WIDTH , ACQ_HEIGHT ) , CV_8U );
cv::Mat dx;
// Point Mat to buffer data
src.data = (uchar*)(bufObject->address);
// Calculate x-gradient
cv::Sobel( src , dx , CV_16U , 1 , 0 , CV_SCHARR , 1 , 0 , BORDER_REPLICATE ) ;
But when I apply it, it gives be a ROI assertion error:
OpenCV Error: Assertion failed (roi.x >= 0 && roi.y >= 0 && roi.width >= 0 && roi.height >= 0 && roi.x + roi.width <= wholeSize.width && roi.y + roi.height <= wholeSize.height) in start, file /home/nvidia/Desktop/opencv-3.3.1/modules/imgproc/src/filter.cpp, line 173
terminate called after throwing an instance of 'cv::Exception'
what(): /home/nvidia/Desktop/opencv-3.3.1/modules/imgproc/src/filter.cpp:173: error: (-215) roi.x >= 0 && roi.y >= 0 && roi.width >= 0 && roi.height >= 0 && roi.x + roi.width <= wholeSize.width && roi.y + roi.height <= wholeSize.height in function start
Very strangely (at least in my opinion), when I clone the same matrix and I apply the filter to the clone, everything's fine!
// Create Mats
cv::Mat src = cv::Mat( cv::Size( ACQ_WIDTH , ACQ_HEIGHT ) , CV_8U );
cv::Mat dx;
// Point Mat to buffer data
src.data = (uchar*)(bufObject->address);
// Create clone
cv::Mat cl = src.clone() ;
// Calculate x-gradient
cv::Sobel( cl , dx , CV_16U , 1 , 0 , CV_SCHARR , 1 , 0 , BORDER_REPLICATE ) ;
The data source is ok: both imshow(src) and imshow (fm) correctly show the buffer, and as mentioned before, if using the clone instead of the original image everything works.
Since this is a time-sensitive application and this is a video-frame loop, I don't want to clone the source at every step. What am I missing?
UPDATE:
Both source and clone Mat give TRUE if checked with isContinuous() function.
total(), elemSize(), cols, rows, dims and flags give the same results for both source and clone

How to detect squares in a video using openCV for iOS?

I'm trying to detect squares shape in a video, application crashes and showing following error.
OpenCV Error: Assertion failed (j < nsrcs && src[j].depth() == depth) in mixChannels, file /Users/alexandershishkov/opencv2.4.3rc/opencv/modules/core/src/convert.cpp, line 472
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/alexandershishkov/opencv2.4.3rc/opencv/modules/core/src/convert.cpp:472: error: (-215) j < nsrcs && src[j].depth() == depth in function mixChannels
here is the code.
vector<vector<cv::Point> > squares;
cvtColor(image,image,CV_BGR2GRAY);
GaussianBlur(image,image,cv::Size(9,11),0,0);
find_squares(image, contours);
void find_squares(Mat& image, vector<vector<cv::Point> >& squares)
{
Mat blurred(image);
medianBlur(image, blurred, 9);
Mat gray0(blurred.size(), CV_8U), gray;
vector<vector<cv::Point> > contours;
for (int c = 0; c < 3; c++)
{
int ch[] = {c, 0};
mixChannels(&blurred, 1, &gray0, 1, ch, 1);
const int threshold_level = 2;
for (int l = 0; l < threshold_level; l++)
{
if (l == 0)
{
Canny(gray0, gray, 10, 20, 3);
dilate(gray, gray, Mat(), cv::Point(-1,-1));
}
else
{
gray = gray0 >= (l+1) * 255 / threshold_level;
}
findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
vector<cv::Point> approx;
for (size_t i = 0; i < contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);
if (approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;
for (int j = 2; j < 5; j++)
{
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}
if (maxCosine < 0.3)
squares.push_back(approx);
}
}
}
}
}
blurred and gray0 are 1-channel images. So, you try to copy second and third channels of blurred image which does not exit! The error should be because of this.
I hope it helps. I do not have any idea about rest of the code and what do you want to do.

Calculating image gradient direction using OpenCV's Sobel operation

I am attempting to determine the image gradient direction using the results from OpenCV's Sobel method.
I understand this should be a very simple task, I think I understand the theory but implementing this has been more challenging than I thought.
I would expect the gradient directions to be between 0-360 degrees, however my code shows all gradients fall between 180 - 270 degrees.
I submitted a previous version of this code which included an integer division issue. I have fixed this but it has not solved the problem of a restricted angle of direction.
I have stepped through all the code but I just can't see where I'm going wrong? Can anyone spot my mistake?
Thanks.
void getGradients(IplImage* original, cv::Mat* gradArray)
{
cv::Mat original_Mat(original, true);
// Convert it to gray
cv::cvtColor( original_Mat, original_Mat, CV_RGB2GRAY );
//cv::blur(original_Mat, original_Mat, cv::Size(7,7));
/// Generate grad_x and grad_y
cv::Mat grad_x = cv::Mat::zeros(original->height, original->width, CV_16S);
cv::Mat grad_y = cv::Mat::zeros(original->height, original->width, CV_16S);
/// Gradient X
cv::Sobel(original_Mat, grad_x, CV_16S, 1, 0, 3);
/// Gradient Y
cv::Sobel(original_Mat, grad_y, CV_16S, 0, 1, 3);
uchar* pixelX = grad_x.data;
uchar* pixelY = grad_y.data;
uchar* grad1 = gradArray[0].data;
uchar* grad2 = gradArray[1].data;
uchar* grad3 = gradArray[2].data;
uchar* grad4 = gradArray[3].data;
uchar* grad5 = gradArray[4].data;
uchar* grad6 = gradArray[5].data;
uchar* grad7 = gradArray[6].data;
uchar* grad8 = gradArray[7].data;
int count = 0;
int min = 999999;
int max = -1;
for(int i = 0; i < grad_x.rows * grad_x.cols; i++)
{
double directionRAD = atan2(pixelY[i], pixelX[i]);
int directionDEG = (int)(180 + directionRAD / M_PI * 180);
if(directionDEG < min){min = directionDEG;}
if(directionDEG > max){max = directionDEG;}
if(directionDEG >= 0 && directionDEG <= 45) { grad1[i] = 255; count++;}
if(directionDEG >= 45 && directionDEG <= 90) { grad2[i] = 255; count++;}
if(directionDEG >= 90 && directionDEG <= 135) { grad3[i] = 255; count++;}
if(directionDEG >= 135 && directionDEG <= 190) { grad4[i] = 255; count++;}
if(directionDEG >= 190 && directionDEG <= 225) { grad5[i] = 255; count++;}
if(directionDEG >= 225 && directionDEG <= 270) { grad6[i] = 255; count++;}
if(directionDEG >= 270 && directionDEG <= 315) { grad7[i] = 255; count++;}
if(directionDEG >= 315 && directionDEG <= 360) { grad8[i] = 255; count++;}
if(directionDEG < 0 || directionDEG > 360)
{
cout<<"Weird gradient direction given in method: getGradients.";
}
}
}
grad_x and grad_y are Mats of type CV_16SC1, that is every pixel in them takes up two bytes.
However you declare pixelX and pixelY to pointers to 8 bit bytes. Therefore pixelX[1] is the second byte of the first gradient, rather than the second gradient.
You need
short* pixelX = grad_x.ptr<short>(0);
short* pixelY = grad_y.ptr<short>(0);
Problem is here
uchar* pixelX = grad_x.data;
uchar* pixelY = grad_y.data;
and here
double directionRAD = atan2(pixelY[i], pixelX[i]);
You don`t take abs(), but use unsigned pointer. That is why you cannot get x or y negative.
Should be:
short* pixelX = (short*) grad_x.data;
short* pixelY = (short*) grad_y.data;
and
double directionRAD = atan2((double)pixelY[i], (double)pixelX[i]);

How to perform skin tone matching

( face )
( body )
Hi, i am new to image processing and openCV C/C++. I am wondering that is it possible to extract skin tone from the first image (face). And then applied to the second image (body).
In other words, user upload his face image and the program extract the skin tone from that image and apply it to the body.
Thanks,
Aisha
This is a hard problem to solve, especially given the variation of colours depending on lighting and reflection. I have worked previously on finding skin in images, and generally the Cr (chroma red) component of the YCbCr colour space stands out prominently on skin. You might be able to exploit this information to find skin regions.
Here are a couple of papers that attempt to use colour for locating human skin:
1. Interaction between hands and wearable cameras
2. Markerless inspection of augmented reality objects
For finding skin you can use one of this formulas:
1) With normilized RGB space:
for(int i = 0; i < m_image->height; ++i)
{
for(int j = 0; j < m_image->width; ++j)
{
if (m_image->nChannels == 3)
{
int valueR = (reinterpret_cast<uchar*>(m_image->imageData + i * m_image->widthStep))[j * 3 + 2];
int valueG = (reinterpret_cast<uchar*>(m_image->imageData + i * m_image->widthStep))[j * 3 + 1];
int valueB = (reinterpret_cast<uchar*>(m_image->imageData + i * m_image->widthStep))[j * 3];
float normR = static_cast<float>(valueR) / static_cast<float>(valueR + valueG + valueB);
float normG = static_cast<float>(valueG) / static_cast<float>(valueR + valueG + valueB);
float normB = static_cast<float>(valueB) / static_cast<float>(valueR + valueG + valueB);
if ((normB / normG < 1.249) &&
(( normR + normG + normB ) / ( 3 * normR ) > 0.696 ) &&
( 1/3.0 - normB/( normR + normG + normB ) > 0.014 ) &&
(normG/(3* (normR + normG + normB)) < 0.108 ))
{
//pixel is skin
}
}
}
2) in RGB space:
for(size_t i = 0; i < m_image->height; ++i)
{
for(size_t j = 0; j < m_image->width; ++j)
{
if (m_image->nChannels == 3)
{
int R = (reinterpret_cast<uchar*>(m_image->imageData + i * m_image->widthStep))[j * 3 + 2];
int G = (reinterpret_cast<uchar*>(m_image->imageData + i * m_image->widthStep))[j * 3 + 1];
int B = (reinterpret_cast<uchar*>(m_image->imageData + i * m_image->widthStep))[j * 3];
if (( R > 95) && ( G > 40 ) && ( B > 20 ) &&
(std::max(R, std::max( G, B) ) - std::min(R, std::min(G, B) ) > 15) &&
(std::abs(R - G) > 15) && (R > G) && (R > B))
{
//skin pixel
}
}
}
3) in YCrCb space:
for(size_t i = 0; i < m_image->height; ++i)
{
for(size_t j = 0; j < m_image->width; ++j)
{
if (m_image->nChannels == 3)
{
int Cr = (reinterpret_cast<uchar*>(image->imageData + i * image->widthStep))[j * 3 + 2];
int Cb = (reinterpret_cast<uchar*>(image->imageData + i * image->widthStep))[j * 3 + 1];
int Y = (reinterpret_cast<uchar*>(image->imageData + i * image->widthStep))[j * 3];
if (( Y > 80 ) && ( Cb > 85 ) && ( Cb < 135 ) &&
(Cr > 135) && (Cr < 180))
{
//skin pixel
}
}
}
}

Locating multiple blobs in an image

For a computer science class, I'm supposed to write a program that will find the position and the size of an unknown number of aliens in a picture like this one: http://www-bcf.usc.edu/~stejada/csci101/Pix/MARS2.jpg. I currently have the following code:
#include "Myro.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "alien.h"
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
void trackBlob(PicturePtr &aPic, int x, int y);
int BlobSize(PicturePtr &aPic, int x, int y, vector<int>& xLocs, vector<int>& yLocs);
vector<alien> found;
int returned;
int main()
{
returned = 0;
cout << "Pick a picture to search 0-5, or choose 6 to quit" << endl;
int selection;
cin >> selection;
PicturePtr thePic;
// Choose a picture to search
bool selecting = true;
while (selecting) {
switch (selection) {
case 0:
thePic = makePicture("MARS.jpg");
selecting = false;
break;
case 1:
thePic = makePicture("MARS1.jpg");
selecting = false;
break;
case 2:
thePic = makePicture("MARS2.jpg");
selecting = false;
break;
case 3:
thePic = makePicture("MARS3.jpg");
selecting = false;
break;
case 4:
thePic = makePicture("MARS4.jpg");
selecting = false;
break;
case 5:
thePic = makePicture("MARS6.jpg");
selecting = false;
break;
case 6:
cout << "Terminating." << endl;
return 0;
default:
cout << "Invalid input. Please try again." << endl << endl;
break;
}// end switch
} // end while
// Find the aliens
cout << endl;
cout << "Pic size: " << getHeight(thePic) * getWidth(thePic) << endl << endl;
int numGreen = 0;
for (int i = 0; i < getWidth(thePic); i++) {
for (int j = 0; j < getHeight(thePic); j++) {
Pixel pix;
pix = getPixel(thePic,i,j);
// Check for alien color
if (pix.R >= 100 && pix.R <= 120 && pix.G >= 240 && pix.G <= 255 && pix.B >= 1 && pix.B <= 15) {
trackBlob(thePic,i,j);
numGreen++;
}
else if (pix.R >= 165 && pix.R <= 185 && pix.G >= 215 && pix.G <= 235 && pix.B >= 65 && pix.B <= 85) {
trackBlob(thePic,i,j);
numGreen++;
}
else if (pix.R >= 130 && pix.R <= 155 && pix.G >= 210 && pix.G <= 230 && pix.B >= 145 && pix.B <= 175) {
trackBlob(thePic,i,j);
numGreen;
}
else if (pix.R >= 27 && pix.R <= 47 && pix.G <= 265 && pix.G >= 245 && pix.B >= 1 && pix.B <= 10) {
trackBlob(thePic,i,j);
numGreen++;
}
else if (pix.R >= 240 && pix.R <= 255 && pix.G <= 240 && pix.G >= 255 && pix.B >= 25 && pix.B <= 45) {
trackBlob(thePic,i,j);
numGreen++;
}
else if (pix.R >= 235 && pix.R <= 255 && pix.G <= 245 && pix.G >= 210 && pix.B >= 200 && pix.B <= 230) {
trackBlob(thePic,i,j);
numGreen++;
}
else if (pix.R <= 240 && pix.R >= 210 && pix.G <=255 && pix.G >= 240 && pix.B >= 1 && pix.B <= 10) {
trackBlob(thePic,i,j);
numGreen++;
}
}
}
cout << endl << endl;
cout << "Number of Aliens: " << found.size() << endl;
for (int n = 0; n < found.size(); n++) {
cout << "Position of Alien #" << n << ": (" << found[n].x << "," << found[n].y << ")" << endl;
cout << "Size of Alien #" << n << ": " << found[n].size << endl;
}
cout << endl << endl << "Number pixels tried: " << numGreen << endl << endl;
cout << "Times blob returned: " << returned << endl;
show(thePic);
return 0;
}
void trackBlob(PicturePtr &aPic, int x, int y) {
// Just to be safe
Pixel aPixel = getPixel(aPic,x,y);
//if (aPixel.R == 0 && aPixel.G == 0 && aPixel.B == 0)
//return;
// Find the size of the alien
vector<int> xLocs;
vector<int> yLocs;
int size = BlobSize(aPic,x,y,xLocs,yLocs);
cout << "Blob size: " << size << endl;
returned++;
// Make sure it's big enough to be an alien
if (size < 5) {
return;
}
alien newAlien;
// Find the position
newAlien.x = 0;
for (int i = 0; i < xLocs.size(); i++) {
newAlien.x += xLocs[i];
}
newAlien.x = newAlien.x/xLocs.size();
newAlien.y = 0;
for (int j = 0; j < yLocs.size(); j++) {
newAlien.y += yLocs[j];
}
newAlien.y = newAlien.y/yLocs.size();
found.push_back(newAlien);
}
int BlobSize(PicturePtr &aPic, int x, int y, vector<int>& xLocs, vector<int>& yLocs)
{
if (x >= getWidth(aPic) || x < 0)
return 0;
Pixel pix = getPixel(aPic,x,y);
// Just to be safe
//if (pix.R == 0 && pix.G == 0 && pix.B == 0)
//return 0;
bool isAlien = false;
if (pix.R >= 100 && pix.R <= 120 && pix.G >= 240 && pix.G <= 255 && pix.B >= 1 && pix.B <= 15)
isAlien = true;
else if (pix.R >= 165 && pix.R <= 185 && pix.G >= 215 && pix.G <= 235 && pix.B >= 65 && pix.B <= 85)
isAlien = true;
else if (pix.R >= 130 && pix.R <= 155 && pix.G >= 210 && pix.G <= 230 && pix.B >= 145 && pix.B <= 175)
isAlien = true;
else if (pix.R >= 27 && pix.R <= 47 && pix.G <= 265 && pix.G >= 245 && pix.B >= 1 && pix.B <= 10)
isAlien = true;
else if (pix.R >= 240 && pix.R <= 255 && pix.G <= 240 && pix.G >= 255 && pix.B >= 25 && pix.B <= 45)
isAlien = true;
else if (pix.R >= 235 && pix.R <= 255 && pix.G <= 245 && pix.G >= 210 && pix.B >= 200 && pix.B <= 230)
isAlien = true;
else if (pix.R <= 240 && pix.R >= 210 && pix.G <=255 && pix.G >= 240 && pix.B >= 1 && pix.B <= 10)
isAlien = true;
if (!isAlien)
return 0;
// Store the location in the position vectors
xLocs.push_back(x);
yLocs.push_back(y);
// Make sure the pixel doesn't get counted again
setPixelColor(aPic,x,y,0,0,0);
int size = 0;
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 2; j++) {
if (i == 0 && j == 0) {
break;
}
size += BlobSize(aPic,x + (i-1), y + (j-1),xLocs,yLocs);
}
}
return 1 + size;
}
When I run the code, it says it's found several hundred "aliens." Each one it finds is bigger than the last if that's helpful. I've been staring at this for days and have no idea what's going wrong. Any help would be much appreciated.
Thanks!
First off, you should remove the comments from BlobSize() like so:
// So pixels aren't counted twice
if (pix.R == 0 && pix.G == 0 && pix.B == 0)
return 0;
Since BlobSize() changes the counted pixels to black, you need to keep those lines in your code so it doesn't count pixels twice.
Also, your for loop at the end of BlobSize() is slightly wrong. It should look like this:
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
// skip pixel at this location
if (i == 0 && j == 0) {
continue;
}
size += BlobSize(aPic,x + (i-1), y + (j-1),xLocs,yLocs);
}
}
The way that it was before would check the pixel one above and left of the pixel at the current location, see that i == 0 && j == 0, then skip checking the first column. The program would run BlobSize() on the other 6 pixels (including the one at the current location).
By the way, nice use of vectors and recursion. Sorry if I answered too late and you already turned it in. I'm turning mine in on Sunday because of the extension.

Resources