我正在尝试编写一个使用opencv从图像读取像素值并将其打印在控制台或其他窗口中的程序。我的程序已成功编译并链接,但是在Visual stdio中运行该程序时,出现以下错误提示。
“ Project1.exe中0x00007FFCA550A839处出现未处理的异常:Microsoft C ++异常:内存位置0x0000002F82CFEF60处的std :: bad_alloc。”
我已经尝试直接运行可执行文件。然后我收到此错误消息“调试错误。about()已被调用”。 这是我的main.cpp代码
'''c ++
#include <stdlib.h>
#include <SDL.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
using namespace cv;
int retColorRed(int x, int y)
{
Mat img = imread("s1.jpg", 0);
return img.at<Vec3d>(x, y)[0];
}
int retColorGreen(int x, int y)
{
Mat img = imread("s1.jpg", 0);
return img.at<Vec3b>(x, y)[1];
}
int retColorBlue(int x, int y)
{
Mat img = imread("s1.jpg", 0);
return img.at<Vec3b>(x, y)[2];
}
#define WINDOW_WIDTH 200
#define WINDOW_HEIGHT 300
int main(int argc, char* args[]) {
SDL_Event event;
SDL_Renderer* renderer;
SDL_Window* window;
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0,&window, &renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
int width = WINDOW_WIDTH;
int height = WINDOW_HEIGHT;
int horizonaltIter = 0, virticalIter = 0;
while (virticalIter <= height)
{
while (horizonaltIter <= width) {
int red = retColorRed(horizonaltIter,virticalIter);
int green = retColorGreen(horizonaltIter, virticalIter);
int blue = retColorBlue(horizonaltIter, virticalIter);
SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
SDL_RenderDrawPoint(renderer, horizonaltIter, virticalIter);
SDL_RenderPresent(renderer);
horizonaltIter++;
}
virticalIter++;
}
while (1) {
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
break;
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
'''
这是Visual Studio ide中显示的错误消息
“ Project1.exe中0x00007FFCA550A839处出现未处理的异常:Microsoft C ++异常:内存位置0x000000A6BD0FF1A0处的std :: bad_alloc。”
答案 0 :(得分:0)
将while (horizonaltIter <= height) {
更改为while (horizonaltIter <= width) {
,它应该可以工作!