mercredi 1 avril 2015

How to improve the processing of images in cpp?

I have ~1000 images (640x640) from a x-ray-ct scan, and each of the images represent a slice of my scanned object. Now I wanted to create a 3d-model out of these images (as already described here). For that I wanted to put all of the image data into a 3d-matrix, where the x- and y-coordinate gives the colour of a picture at this point, while the z-coordinate is the image number. I created the following algorithm:



//Read all images
QPixmap tmpImg;
QImage tmpImgMap;
QPair<int, int> pos;
pixel Pixel;
QString imagePos;
QPair<int, QVector<pixel> > image;
QColor colour;
for(int i = 0; i < newProcess->images.size(); i++)
{
tmpImg.load(newProcess->images[i]);
tmpImgMap = tmpImg.toImage();
imagePos = newProcess->images[i];
imagePos.remove("img/USB_proc");
imagePos.remove(".png");
int position = imagePos.toInt();
image.first = position;
image.second.clear();
for(int j = 0; j < tmpImgMap.width(); j++)
{

for(int h = 0; h < tmpImgMap.height(); h++)
{
pos = qMakePair(j, h);
Pixel = qMakePair(pos, true);
colour = QColor::fromRgb(Pixel.second);
if(colour.blue() + colour.red() + colour.green() != 0)
{
image.second.push_back(Pixel);
ui->readNumber->setText(QString::number(this->images.size()));
}
else
{
Pixel = qMakePair(pos, false);
image.second.push_back(Pixel);
}
}
}
this->images.push_back(image);
}
ui->readNumber->setText(QString::number(this->images.size()));
ui->lineEdit->setText("Länge des images-arrays ist: " + QString::number(this->images.size()));
qSort(this->images.begin(), this->images.end(), sortImages);
ui->readNumber->setText("Sorted!");


with the earlier defined variables newProcess->images as a vector containing the path to the images and this->images as QVector<QPair<int, QVector<pixel> > > images. pixel has been defined as typedef QPair<QPair<int, int>, bool> pixel.

The idea behind the algorithm is: Go through all images, get all pixels, look up the colour, if it is black, then store false, else true (done for speed and storage). Store in a pixel-value the colour and the position of one pixel, and put them together (after finishing the loop over all pixels in one image) into a image-vector. This image-vector is finally saved in this->images.

My problem is: This algorithm is extremely slow, one image takes around one second. Furthermore it needs processing afterwards to get it into a 3d-matrix. How can I improve the speed of this algorithm (and preferably get the original colour back, i.e. that I don't have to use bool-values, but QRgb-Values instead?


Aucun commentaire:

Enregistrer un commentaire