C++ C++;将自移除推回向量时崩溃
C++ C++;将自移除推回向量时崩溃,c++,memory,vector,crash,segmentation-fault,C++,Memory,Vector,Crash,Segmentation Fault,我想从基因上重建给定的图像 我尝试在sfml中执行此操作。 我已经创建了一个自我分离的正方形,它试图进化成看起来 喜欢源图像 不幸的是,这个东西崩溃了,我不知道为什么,我想一切都处理得很好,向量附加应该不会是问题。 请查看代码: 主要功能: #include "divisablesquare.h" #include <SFML/Graphics.hpp> #include <iostream> #include <cstring> #include <s
我想从基因上重建给定的图像
我尝试在sfml中执行此操作。
我已经创建了一个自我分离的正方形,它试图进化成看起来
喜欢源图像
不幸的是,这个东西崩溃了,我不知道为什么,我想一切都处理得很好,向量附加应该不会是问题。
请查看代码:
主要功能:
#include "divisablesquare.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstring>
#include <string>
#include <error.h>
#include <algorithm>
namespace GLOBAL
{
bool DEBUG_MODE = false;
};
int IDX = 0;
int main(int argc, char * argv[])
{
srand(time(NULL));
std::string def;
for(int i = 1; i < argc; i++)
{
def = argv[i];
std::string def2 = def;
std::transform(def2.begin(), def2.end(), def2.begin(), ::tolower);
if(strcmp(def2.c_str(), "--debug") == 0)
{
GLOBAL::DEBUG_MODE = true;
std::cerr << "Running in debug mode" << std::endl;
}
else
{
break;
}
}
sf::Image sourceImage;
sf::Texture sample;
if(!sourceImage.loadFromFile(def) && GLOBAL::DEBUG_MODE)
{
std::cerr << "Failed to open specified image!" << std::endl;
}
sample.loadFromImage(sourceImage);
sf::RectangleShape sourceRect;
sourceRect.setSize((sf::Vector2f)sourceImage.getSize());
sourceRect.setTexture(&sample);
sf::RenderWindow mainWindow(sf::VideoMode(sourceImage.getSize().x*2+10, sourceImage.getSize().y), "Genetic Image Generator");
std::vector<DivisableSquare> dSquares;
{
DivisableSquare starter(&dSquares, &sourceImage);
starter.init(128, 128, 128, sourceImage.getSize().x, sourceImage.getSize().y, sourceImage.getSize().x + 10, 0);
starter.Shape.setPosition({(float)sourceImage.getSize().x + 10, 0});
starter.Shape.setFillColor({128,128,128});
starter.Shape.setSize({(float)sourceImage.getSize().x, (float)sourceImage.getSize().y});
dSquares.push_back(starter);
}
sf::Clock clock;
while(mainWindow.isOpen())
{
sf::Time elapsed = clock.getElapsedTime();
if(elapsed.asMilliseconds() > 1000)
{
clock.restart();
dSquares.at(rand() % dSquares.size()).Partup();
}
sf::Event mainEvent;
while(mainWindow.pollEvent(mainEvent))
{
if(mainEvent.type == sf::Event::Closed)
mainWindow.close();
}
mainWindow.clear();
mainWindow.draw(sourceRect);
for(auto &&ref: dSquares)
{
mainWindow.draw(ref.Shape);
}
mainWindow.display();
}
}
#包括“divisiablesquare.h”
#包括
#包括
#包括
#包括
#包括
#包括
命名空间全局
{
bool DEBUG_MODE=false;
};
int-IDX=0;
int main(int argc,char*argv[])
{
srand(时间(空));
std::字符串定义;
对于(int i=1;i红色=r;
这个->蓝色=b;
这->绿色=g;
这个->宽度=宽度;
这个->高度=高度;
这->posX=posX;
这个->posY=posY;
}
void divisiablesquare::Partup()
{
如果(此->宽度<2&&此->高度<2)
返回;
双百分比切割=(兰德()%60+20)/100;
bool horizontalCut=rand()%2;
双posX1,posX2;
双位置1,位置2;
双宽度1,宽度2;
双倍高度1,高度2;
if(水平切割)
{
posX1=此->posX;
posX2=(此->posX+此->宽度)*切割百分比;
posY1=此->posY;
posY2=此->posY;
宽度1=此->宽度*切割百分比;
宽度2=此->宽度*(1%切割);
高度1=此->高度;
高度2=此->高度;
}
其他的
{
posX1=此->posX;
posX2=此->posX;
posY1=此->posY;
posY2=(此->posY+此->高度)*切割百分比;
宽度1=此->宽度;
宽度2=此->宽度;
高度1=此->高度*切割百分比;
高度2=此->高度*(1%切割);
}
结构RGB
{
浮子r,g,b;
浮点数;
漂浮原材料;
浮动平均CMP;
/**
*请确保稍后追加originalCmp
*同时删除“=0”
*完成
*/
};
std::矢量原始像素1;
std::矢量原始像素2;
for(无符号整数i=posX1;iparentImage->getSize().x>i&&this->parentImage->getSize().y>j)
{
RGB像素;
pixel.r=this->parentImage->getPixel(i,j).r;
pixel.g=this->parentImage->getPixel(i,j).g;
pixel.b=this->parentImage->getPixel(i,j).b;
原始像素1.向后推(像素);
}
}
}
for(无符号整数i=posX2;iparentImage->getSize().x>i&&this->parentImage->getSize().y>j)
{
RGB像素;
pixel.r=this->parentImage->getPixel(i,j).r;
pixel.g=this->parentImage->getPixel(i,j).g;
pixel.b=this->parentImage->getPixel(i,j).b;
原始像素2.向后推(像素);
}
}
}
RGB pix1={0,0,0,0,0},pix2={0,0,0,0,0};
用于(自动和参考:originalPixels1)
{
pix1.r+=ref.r;
pix1.g+=参考g;
pix1.b+=参考b;
}
pix1.r/=originalPixels1.size();
pix1.g/=originalPixels1.size();
pix1.b/=originalPixels1.size();
用于(自动和参考:originalPixels2)
{
pix2.r+=参考r;
pix2.g+=参考g;
pix2.b+=参考b;
}
pix2.r/=originalPixels1.size();
pix2.g/=originalPixels1.size();
pix2.b/=原始像素1.size();
自动比较值=[](RGB v1,RGB v2)
{
float val1=0.2126*v1.r+0.7152*v1.g+0.0722*v1.b;
浮点数val2=0.2126*v2.r+0.7152*v2.g+0.0722*v2.b;
返回(val1>val2)?val1-val2:val2-val1;
};//越小越好
RGB优先[100];
RGB秒[100];
对于(int i=0;i<100;i++)
{
第一个[i].r=rand()%255;
第一个[i].g=rand()%255;
第一个[i].b=rand()%255;
第二个[i].r=rand()%255;
第二个[i].g=rand()%255;
第二个[i].b=rand()%255;
}
//在此处插入orginalcmp
对于(int i=0;i<100;i++)
{
第一个[i].originalCmp=comparVal(第一个[i],pix1);
第二个[i],originalCmp=comparVal(第二个[i],pix2);
}
RGB pRgb;
pRgb.r=此->红色;
pRgb.b=此->蓝色;
pRgb.b=此->蓝色;
对于(int i=0;i<100;i++)
{
first[i].parentCmp=comparVal(first[i],pRgb);
第二个[i].parentCmp=comparVal(第二个[i],pRgb);
first[i].averageCmp=(first[i].originalCmp+first[i].parentCmp)/2;
second[i].averageCmp=(second[i].originalCmp+second[i].parentCmp)/2;
}
std::sort(first,first+100,[](constrgb&l,constrgb&r){返回r.averageCmp>l.averageCmp;});
std::sort(second,second+100,[](constrgb&l,constrgb&r){返回r.averageCmp>l.averageCmp;});
RGB bestfirst=first[rand()%10],bestsecond=second[rand()%10];
DivisableSquare firstSQ(此->ParentContainer,此->parentImage);
DivisableSquare secondSQ(此->父容器,此->父映像);
firstSQ.init(bestfirst.r、bestfirst.g、bestfirst.b、宽度1、高度1、位置X1、位置Y1);
secondSQ.init(最佳秒r、最佳秒g、最佳秒b、宽度2、高度2、位置x2、位置y2);
firstSQ.Shape.setFillColor({(sf::Uint8)bestfirst.r,
#ifndef DIVISABLESQUARE_H
#define DIVISABLESQUARE_H
#include <vector>
#include <SFML/Graphics.hpp>
class DivisableSquare
{
private:
sf::Image * parentImage;
std::vector<DivisableSquare> * ParentContainter;
unsigned short red, green, blue;
double width, height;
double posX, posY;
int id;
public:
~DivisableSquare();
sf::RectangleShape Shape;
DivisableSquare(std::vector<DivisableSquare>*, sf::Image*);
void init(unsigned short, unsigned short, unsigned short, unsigned int, unsigned int, unsigned int, unsigned int);
void Partup();
};
#endif // DIVISABLESQUARE_H
#include "divisablesquare.h"
#include <random>
#include <algorithm>
#include <iostream>
extern int IDX;
DivisableSquare::DivisableSquare(std::vector<DivisableSquare> *pc, sf::Image*tp)
{
this->ParentContainter = pc;
this->parentImage = tp;
this->id = IDX;
IDX++;
}
DivisableSquare::~DivisableSquare()
{
}
void DivisableSquare::init(unsigned short r, unsigned short g, unsigned short b,
unsigned int width, unsigned int height, unsigned int posX, unsigned int posY)
{
this->red = r;
this->blue = b;
this->green = g;
this->width = width;
this->height = height;
this->posX = posX;
this->posY = posY;
}
void DivisableSquare::Partup()
{
if(this->width < 2 && this->height < 2)
return;
double percentCut = (rand()%60 + 20)/100;
bool horizontalCut = rand()%2;
double posX1, posX2;
double posY1, posY2;
double width1, width2;
double height1, height2;
if(horizontalCut)
{
posX1 = this->posX;
posX2 = (this->posX+this->width)*percentCut;
posY1 = this->posY;
posY2 = this->posY;
width1 = this->width*percentCut;
width2 = this->width*(1-percentCut);
height1 = this->height;
height2 = this->height;
}
else
{
posX1 = this->posX;
posX2 = this->posX;
posY1 = this->posY;
posY2 = (this->posY + this->height)*percentCut;
width1 = this->width;
width2 = this->width;
height1 = this->height*percentCut;
height2 = this->height*(1-percentCut);
}
struct RGB
{
float r, g, b;
float parentCmp;
float originalCmp;
float averageCmp;
/**
* Make sure to append originalCmp later
* also remove "= 0"
* DONE
*/
};
std::vector<RGB> originalPixels1;
std::vector<RGB> originalPixels2;
for(unsigned int i = posX1; i < posX1+width1; i++)
{
for(unsigned int j = posY1; j < posY1+height1; j++)
{
if(this->parentImage->getSize().x > i && this->parentImage->getSize().y > j)
{
RGB pixel;
pixel.r = this->parentImage->getPixel(i, j).r;
pixel.g = this->parentImage->getPixel(i, j).g;
pixel.b = this->parentImage->getPixel(i, j).b;
originalPixels1.push_back(pixel);
}
}
}
for(unsigned int i = posX2; i < posX2+width2; i++)
{
for(unsigned int j = posY2; j < posY2+height2; j++)
{
if(this->parentImage->getSize().x > i && this->parentImage->getSize().y > j)
{
RGB pixel;
pixel.r = this->parentImage->getPixel(i, j).r;
pixel.g = this->parentImage->getPixel(i, j).g;
pixel.b = this->parentImage->getPixel(i, j).b;
originalPixels2.push_back(pixel);
}
}
}
RGB pix1 = {0,0,0,0,0,0}, pix2={0,0,0,0,0,0};
for(auto &&ref : originalPixels1)
{
pix1.r += ref.r;
pix1.g += ref.g;
pix1.b += ref.b;
}
pix1.r /= originalPixels1.size();
pix1.g /= originalPixels1.size();
pix1.b /= originalPixels1.size();
for(auto &&ref : originalPixels2)
{
pix2.r += ref.r;
pix2.g += ref.g;
pix2.b += ref.b;
}
pix2.r /= originalPixels1.size();
pix2.g /= originalPixels1.size();
pix2.b /= originalPixels1.size();
auto comparVal = [](RGB v1, RGB v2)
{
float val1 = 0.2126*v1.r + 0.7152*v1.g + 0.0722*v1.b;
float val2 = 0.2126*v2.r + 0.7152*v2.g + 0.0722*v2.b;
return (val1 > val2) ? val1-val2 : val2-val1;
};//smaller - better
RGB first[100];
RGB second[100];
for(int i = 0; i < 100; i++)
{
first[i].r = rand() % 255;
first[i].g = rand() % 255;
first[i].b = rand() % 255;
second[i].r = rand() % 255;
second[i].g = rand() % 255;
second[i].b = rand() % 255;
}
// insert orginalcmp here
for(int i = 0; i < 100; i++)
{
first[i].originalCmp = comparVal(first[i], pix1);
second[i].originalCmp = comparVal(second[i], pix2);
}
RGB pRgb;
pRgb.r = this->red;
pRgb.b = this->blue;
pRgb.b = this->blue;
for(int i = 0; i < 100; i++)
{
first[i].parentCmp = comparVal(first[i], pRgb);
second[i].parentCmp = comparVal(second[i], pRgb);
first[i].averageCmp = (first[i].originalCmp+first[i].parentCmp)/2;
second[i].averageCmp = (second[i].originalCmp+second[i].parentCmp)/2;
}
std::sort(first, first+100, [](const RGB& l, const RGB& r){return r.averageCmp > l.averageCmp;});
std::sort(second, second+100, [](const RGB& l, const RGB& r){return r.averageCmp > l.averageCmp;});
RGB bestfirst = first[rand()%10], bestsecond = second[rand()%10];
DivisableSquare firstSQ(this->ParentContainter, this->parentImage);
DivisableSquare secondSQ(this->ParentContainter, this->parentImage);
firstSQ.init(bestfirst.r, bestfirst.g, bestfirst.b, width1, height1, posX1, posY1);
secondSQ.init(bestsecond.r, bestsecond.g, bestsecond.b, width2, height2, posX2, posY2);
firstSQ.Shape.setFillColor({(sf::Uint8)bestfirst.r, (sf::Uint8)bestfirst.g, (sf::Uint8)bestfirst.b});
secondSQ.Shape.setFillColor({(sf::Uint8)bestsecond.r, (sf::Uint8)bestsecond.g, (sf::Uint8)bestsecond.b});
firstSQ.Shape.setSize({(float)width1, (float)height1});
secondSQ.Shape.setSize({(float)width2, (float)height2});
firstSQ.Shape.setPosition({(float)posX1 + this->parentImage->getSize().x + 10, (float)posY1});
secondSQ.Shape.setPosition({(float)posX2 + this->parentImage->getSize().x + 10, (float)posY2});
this->ParentContainter->push_back(firstSQ);
this->ParentContainter->push_back(secondSQ);
//crash here
for(unsigned int i = 0; i < this->ParentContainter->size(); i++)
{
if(this->ParentContainter->at(i).id == this->id)
this->ParentContainter->erase(this->ParentContainter->begin()+i);
}
}