首页 > 解决方案 > 将位图数据转换为 rgb visual c++

问题描述

我正在尝试拍摄位图图像并获取像素的 RGB 值。我目前拥有的将打开位图文件并读取像素数据:

#define _CRT_SECURE_NO_DEPRECATE

#include "findColor.h"

#include <vector>
#include <iostream>
int findColor(std::string path) {
    std::vector<std::string> averageColor; //Will hold the average hex color of each image in order.

    std::string currentImage;


    currentImage = path + std::to_string(i) + ".btm";

    FILE* f = fopen(currentImage.c_str(), "rb");
    unsigned char info[54]; //Bitmap header is 54 bytes
    fread(info, sizeof(unsigned char), 54, f); //reading the header

    // extract image height and width from header
    int width, height;
    memcpy(&width, info + 18, sizeof(int));
    memcpy(&height, info + 22, sizeof(int));
        int heightSign = 1;
    if (height < 0) {
        heightSign = -1;
    }
    int size = 3 * width * height; //size of image in bytes. 3 bytes per pixel.
    unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
    fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
        fclose(f); //close image.

    for (i = 0; i < size; i += 3) //Flip the image data? It is stored as BGR flipping it to RGB?
    {
        unsigned char tmp = data[i-33];
        data[i] = data[i + 2];
        data[i + 2] = tmp;
    }

    return 0;

}

我真的不知道从这里去哪里。任何回应将不胜感激。

标签: c++imagebitmap

解决方案


推荐阅读