PDA

View Full Version : QImage::pixelIndex out of range warning



danics
10th March 2010, 12:12
HI everybody!

i want create Morphological operation Dilation and erosion, i m using Format_Mono for create binary image this is my code:


QImage BinaryImage::createProbe(int width, int height, bool *data[])
{
QImage temp(width,height,QImage::Format_Mono);
for(int i=0; i<width;i++)
for(int j=0; j<height;j++)
temp.setPixel(i,j,(uint) data[i][j]);

return temp;
}

bool **data;
data = new bool*[3];
for(int i=0;i<3;i++)
data[i] = new bool[3];

for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
data[i][j] = 1;

BinaryImage::createProbe(3,3,data);


this work great, i convert a Rgb32 image to Binary image with this code:


void BinaryImage::convertToBinary(QImage* image,float treshholding)
{
QImage temp(image->width(),image->height(),QImage::Format_Mono);

if(image->isNull()) return;

for(int i=0; i < image->height();i++)
{
QRgb* p =(QRgb *)image->scanLine(i);
for(int j=0;j<image->width();j++)
{
int c = (qRed(p[j])+qGreen(p[j])+qBlue(p[j]))/3;
c = (c > treshholding * 1000)? 1 : 0;
temp.setPixel(j,i,c);
}
}

*image = temp;
}


this works great too.

but my function for create dilation image is like this:


Morphology::Morphology(QImage image, QImage probe)
{
if(image.format() == QImage::Format_Mono || probe.format() == QImage::Format_Mono)
{
this->image = image;
this->probe = probe;
}
}

QImage Morphology::applyDilation()
{
QImage dest(image.width(),image.height(),QImage::Format_M ono);

QVector<uchar> pix_values;
int i,j;

for(int row =0;row<image.height();row++)
{
//uchar* dst_row_ptr = dest.scanLine(row);
for(int column = 0; column < image.width(); column++)
{
pix_values.clear();
for(int prow = 0; prow < probe.height(); prow++)
{
i = prow + row - probe.height() / 2;
//uchar* img_row_ptr = image.scanLine(i);
//uchar* prb_row_ptr = probe.scanLine(prow);

for(int pcolumn = 0; pcolumn < probe.width(); pcolumn++)
{
j = column + pcolumn - probe.width() / 2;
if(i >= 0 && i < image.height() && j >= 0 && j < image.width())
if(probe.pixelIndex(prow, pcolumn) == 1)
pix_values.push_back(image.pixelIndex(i, j));
}
}
dest.setPixel(row,column,(uint)*std::max_element(p ix_values.begin(), pix_values.end())); //the max goes in the destination
}
}

return dest;
}

//run code is this
BinaryImage::convertToBinary(img,0.17);

bool **data;
data = new bool*[3];
for(int i=0;i<3;i++)
data[i] = new bool[3];

for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
data[i][j] = 1;
Morphology morph(*img,BinaryImage::createProbe(3,3,data));
imageLabel->setPixmap(QPixmap::fromImage(morph.applyDilation() ));
imageLabel->adjustSize();

this function is work too but get a lot of warnings QImage::pixelInde() that is out of range and run program too slow.
what is the problem?
what i can do for i havent any warning and run program fast?

Lykurg
10th March 2010, 12:54
dest.setPixel(row,column,(uint)*std::max_element(p ix_values.begin(), pix_values.end())); //the max goes in the destination
has to be
dest.setPixel(column,row,(uint)*std::max_element(p ix_values.begin(), pix_values.end())); //the max goes in the destination
And some other calls before that line has to be changed also.