Консультация № 188939
14.03.2016, 15:28
0.00 руб.
0 0 0
Здравствуйте! Прошу помощи в следующем вопросе:

Пишу программу для расспознавания колличества силуэтов на картинке! Программа не имеет ошибок, но вместо результата выводит в итоге лишь число, которое я задам в коде для главной переменной, которая именно и отвечает за подсчетю Делаю программу в Qt creator c C++

Приложение:
//#include <QtWidgets/QLabel>
#include <QWidget>
#include <QFile>
#include <iostream>
#include <fstream>
#include <string>
#include <qbuffer.h>
#include <QtGui>
using namespace std;
//The variable specifies the color threshold for image binarization
const int THRESHOLD_COLOR = 13882323;
int countingSilhouettes(string fileName);
int main() {
cout << "Please, enter name of the image: \n";
//@param fileName - name of the input image

string fileName = "";
cin >> fileName;
cout << "There is/are " << countingSilhouettes(fileName)<<" silhouette/s on the image." << endl;
return 0;
}

void Binarization(int **bitMap, int W,int H, QImage *img)
{

QImage* temp_image = img;
// if(3 == temp_image->format())
// {
// *temp_image = img->convertToFormat(QImage::Format_RGB32);
// }
// W = img->width();
// H = img->height();

for (int y=1; y<H; y++)
{
for (int x=1; x<W; x++)
{
RGBTRIPLE row;
QColor col;
int red = row.rgbtRed;
int green = row.rgbtGreen;
int blue = row.rgbtBlue;
if (red > THRESHOLD_COLOR && green > THRESHOLD_COLOR && blue > THRESHOLD_COLOR)
{
bitMap[x][y] = 0;
temp_image->setPixel(x,y,col.rgb());
} else {
bitMap[x][y] = 1;
temp_image->setPixel(x,y,col.rgb());
}
}
}
//return img;
}

void Fill(int x, int y, int oldVal, int newVal,int W, int H, int **bitMap)
{

if(bitMap[x][y] == oldVal && bitMap [x][y] != newVal){
bitMap [x][y] = newVal;
if( x > 0 )
Fill(x + 1, y, oldVal,newVal, W, H, bitMap);
if( x < W)
Fill(x - 1, y, oldVal,newVal, W, H, bitMap);
if( y > 0 )
Fill(x, y - 1, oldVal,newVal, W, H, bitMap);
if( y < H )
Fill(x, y + 1, oldVal,newVal, W, H, bitMap);
}
}
/*
* The function counting the number of silhouettes.
*/
int countingSilhouettes(string fileName)
{
//QWindow qw;
//qw.show();
QString filename = filename.fromStdString(fileName);
QImage *image = new QImage(filename);
QFile file(filename);
file.open(QIODevice::ReadOnly);

QDataStream in(&file);
// Read characters from file and put into bytearray.
QByteArray byteArray;
while(!in.atEnd()) {
char *c = new char[1];
in.readRawData(c,1);
byteArray.push_back(*c);
}
// Read value from bytearray (should be value 2).
QBuffer buf;
buf.setBuffer(&byteArray);
buf.open(QIODevice::ReadOnly);
//image->save(&buf);
//buf.setObjectName(filename);
image->loadFromData(byteArray,0);
int imageHeight = (int)image->height();
int imageWidth = (int)image->width();
//creating array for bitMap
int **bitMap = new int *[imageWidth];
for (int x = 0; x < imageWidth; x++) {
bitMap[x] = new int[imageHeight];
}
//Call function Binarization
Binarization(bitMap,imageWidth,imageHeight, image);
int silhouettesOnImg = 0;// silhouettes counter
//@param - The old value for item
int oldVal = 1;
//@param - The new value for item
int newVal = 1;
//search silhouettes on bitMap
for (int y = 1; y < imageHeight; y++) {
for (int x = 1; x < imageWidth; x++){
if (bitMap[x][y] == 1) {
newVal++;
silhouettesOnImg++;
//Call Fill function
Fill(x,y,oldVal,newVal,imageWidth,imageHeight, bitMap);

}
}
}
for (int x = 0; x < imageWidth; x++){
delete bitMap[x];}
delete []bitMap;
//bitMap = NULL;
delete image;
buf.close();
return silhouettesOnImg;
}

Обсуждение

Форма ответа