C++中读取图片长度和宽度

看到一个用ASP写的读取图片文件的长度、宽度的程序,感觉有点意思,于是用C++也写了一个。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class CImage
{
private:
    long   m_Width;
    long   m_Height;
   
    int get_extension(string fname);
public:
    CImage()
    {
        m_Width = 0;
        m_Height = 0;
       
    };
    void LoadImage(char* fname);
   
    long get_width()
    {
        return m_Width;
    };
   
    long get_height()
    {
        return m_Height;
    };   
   
};

int   CImage::get_extension(string  fname)
{      
    char c = fname.at(fname.length()-1);
    char c2 = fname.at(fname.length()-3);
   
    if ((c == 'f') && (c2 == 'g')){    // file extension name is gif
        return 1;
    }else if ((c == 'g') && (c2 == 'j')){ // file extension name is jpg
        return 2;
    }else if ((c == 'g') && (c2 == 'p')){ // file extension name is png
        return 3;
    }else if ((c == 'p') && (c2 == 'b')){ // file extension name is bmp
        return 4;
    }
    return 0;
}

void   CImage::LoadImage(char *fname)
{       
    m_Width = m_Height = 0;
       
    ifstream ffin(fname, std::ios::binary);
       
    if (!ffin){
        cout<<"Can not open this file."<<endl;
        return;
    }  
    int result = get_extension(fname);
    char s1[2] = {0}, s2[2] = {0};
   
    switch(result)
    {
    case 1:    // gif   
        ffin.seekg(6);        
        ffin.read(s1, 2);
        ffin.read(s2, 2);      
        m_Width = (unsigned int)(s1[1])<<8|(unsigned int)(s1[0]);
        m_Height = (unsigned int)(s2[1])<<8|(unsigned int)(s2[0]);   
        break;
    case 2:    // jpg
        ffin.seekg(164);       
        ffin.read(s1, 2);
        ffin.read(s2, 2);      
        m_Width = (unsigned int)(s1[1])<<8|(unsigned int)(s1[0]);
        m_Height = (unsigned int)(s2[1])<<8|(unsigned int)(s2[0]);   
        break;
    case 3:     // png
        ffin.seekg(17);       
        ffin.read(s1, 2);
        ffin.seekg(2, std::ios::cur);
        ffin.read(s2, 2);    
        m_Width = (unsigned int)(s1[1])<<8|(unsigned int)(s1[0]);
        m_Height = (unsigned int)(s2[1])<<8|(unsigned int)(s2[0]);   
        break;
    case 4:     // bmp      
        ffin.seekg(18);       
        ffin.read(s1, 2);
        ffin.seekg(2, std::ios::cur);
        ffin.read(s2, 2);      
        m_Width = (unsigned int)(s1[1])<<8|(unsigned int)(s1[0]);
        m_Height = (unsigned int)(s2[1])<<8|(unsigned int)(s2[0]);   
        break;
    default:
        cout<<"NO"<<endl;
        break;
    }   
    ffin.close();
};


int main(int argc, char *argv[])
{
    if (argc < 2){
        printf("usage: program imagefilename/n");
        return 0;
    } 
    CImage  test;
    test.LoadImage(argv[1]);
    cout<<"width:"<<test.get_width()<<endl;
    cout<<"height:"<<test.get_height()<<endl;
     
    return 0;
}

下面是使用vs2022的c++写使用D435深度相机测量物品长度宽度的代码,需要安装Intel RealSense SDK 2.0。 ```c++ #include <iostream> #include <librealsense2/rs.hpp> using namespace std; using namespace rs2; int main() { // Initialize the camera pipeline pipe; config cfg; cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 30); cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30); pipe.start(cfg); // Wait for the camera to stabilize for (int i = 0; i < 30; i++) pipe.wait_for_frames(); // Get the depth scale auto depth_sensor = pipe.get_active_profile().get_device().first<rs2::depth_sensor>(); auto depth_scale = depth_sensor.get_depth_scale(); // Get the width and height of the color image auto color_stream = pipe.get_active_profile().get_stream(RS2_STREAM_COLOR).as<video_stream_profile>(); int width = color_stream.width(); int height = color_stream.height(); // Create a pointcloud object pointcloud pc; // Create a colorizer object rs2::colorizer color_map; // Main loop while (1) { // Wait for the next frame auto frames = pipe.wait_for_frames(); // Convert the depth frame to meters auto depth_frame = frames.get_depth_frame().apply_filter(color_map); depth_frame = depth_frame * depth_scale; // Generate the pointcloud from the depth frame pc.map_to(frames.get_color_frame()); auto points = pc.calculate(depth_frame); // Create a new pointcloud object for the filtered points pointcloud filtered_pc; // Filter the points to remove noise auto filtered_points = filtered_pc.filter(points); // Create a new pointcloud object for the convex hull points pointcloud hull_pc; // Create a convex hull from the filtered points auto hull_points = hull_pc.create_convex_hull(filtered_points); // Calculate the width and length of the object float width = hull_points.get_width(); float length = hull_points.get_length(); // Print the results cout << "Width: " << width << " meters" << endl; cout << "Length: " << length << " meters" << endl; } return 0; } ``` 这个代码使用了Intel RealSense SDK 2.0提供的API来初始化相机,获取彩色图像和深度图像,并且计算物品的宽度长度。需要注意的是,这个代码只是一个简单的示例,具体的实现方式可能因为实际场景的不同而有所差别。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值