用c++获取图片上一个特定点的像素坐标

C++语言 码拜 9年前 (2015-05-11) 3663次浏览 0个评论

将一张灰度化二值化后的的一张黑色图片上的一个白色亮点坐标提取出来,存到txt文件中,用c++如何实现?很着急,谢谢大家了。

5分
就存一个坐标 ?
ofstream os(“zz.txt”);
os<<“x=”<<x<<“,y=”<<y<<endl;
os.close();
5分
先取得鼠标点击显示图片的控件的坐标, 在把这个坐标按照图片的长宽换算出来即可
例如:
控件点击点坐标(100,100);
控件的宽高(600,800);
图片宽高(2400,3200);
x = (100 / 600)* 2400;
y = (100 / 800) * 3200;
仅供参考:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;

int main() {
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);

    wstring infilename(L"1.jpg");
    string outfilename("color.txt");

    Bitmap* bmp = new Bitmap(infilename.c_str());
    UINT height = bmp->GetHeight();
    UINT width  = bmp->GetWidth();
    cout << "width " << width << ", height " << height << endl;

    Color color;
    ofstream fout(outfilename.c_str());

    for (UINT y = 0; y < height; y++)
    for (UINT x = 0; x < width ; x++) {
            bmp->GetPixel(x, y, &color);
            fout << x << "," << y << ";"
                 << (int)color.GetRed()   << ","
                 << (int)color.GetGreen() << ","
                 << (int)color.GetBlue()  << endl;
    }

    fout.close();

    delete bmp;
    GdiplusShutdown(gdiplustoken);
    return 0;
}
引用 3 楼 zhao4zhong1 的回复:

仅供参考:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;

int main() {
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);

    wstring infilename(L"1.jpg");
    string outfilename("color.txt");

    Bitmap* bmp = new Bitmap(infilename.c_str());
    UINT height = bmp->GetHeight();
    UINT width  = bmp->GetWidth();
    cout << "width " << width << ", height " << height << endl;

    Color color;
    ofstream fout(outfilename.c_str());

    for (UINT y = 0; y < height; y++)
    for (UINT x = 0; x < width ; x++) {
            bmp->GetPixel(x, y, &color);
            fout << x << "," << y << ";"
                 << (int)color.GetRed()   << ","
                 << (int)color.GetGreen() << ","
                 << (int)color.GetBlue()  << endl;
    }

    fout.close();

    delete bmp;
    GdiplusShutdown(gdiplustoken);
    return 0;
}

真的谢谢您,请问我该如何确定我提出来的点的坐标就是这个白色的亮点呢

引用 3 楼 zhao4zhong1 的回复:

仅供参考:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;

int main() {
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);

    wstring infilename(L"1.jpg");
    string outfilename("color.txt");

    Bitmap* bmp = new Bitmap(infilename.c_str());
    UINT height = bmp->GetHeight();
    UINT width  = bmp->GetWidth();
    cout << "width " << width << ", height " << height << endl;

    Color color;
    ofstream fout(outfilename.c_str());

    for (UINT y = 0; y < height; y++)
    for (UINT x = 0; x < width ; x++) {
            bmp->GetPixel(x, y, &color);
            fout << x << "," << y << ";"
                 << (int)color.GetRed()   << ","
                 << (int)color.GetGreen() << ","
                 << (int)color.GetBlue()  << endl;
    }

    fout.close();

    delete bmp;
    GdiplusShutdown(gdiplustoken);
    return 0;
}
 

用c++获取图片上一个特定点的像素坐标
请问这几条语句是什么意思,怎么提取出来白色亮点的坐标?

http://baike.baidu.com/view/1080380.htm
引用 6 楼 zxh707wk 的回复:

http://baike.baidu.com/view/1080380.htm

请问如果要获取的点是白色的,而图片底色是黑色的,这个具体应该怎么修改程序以实现

引用 7 楼 jty940529 的回复:
Quote: 引用 6 楼 zxh707wk 的回复:

http://baike.baidu.com/view/1080380.htm

请问如果要获取的点是白色的,而图片底色是黑色的,这个具体应该怎么修改程序以实现

问老赵

引用 3 楼 zhao4zhong1 的回复:

仅供参考:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;

int main() {
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);

    wstring infilename(L"1.jpg");
    string outfilename("color.txt");

    Bitmap* bmp = new Bitmap(infilename.c_str());
    UINT height = bmp->GetHeight();
    UINT width  = bmp->GetWidth();
    cout << "width " << width << ", height " << height << endl;

    Color color;
    ofstream fout(outfilename.c_str());

    for (UINT y = 0; y < height; y++)
    for (UINT x = 0; x < width ; x++) {
            bmp->GetPixel(x, y, &color);
            fout << x << "," << y << ";"
                 << (int)color.GetRed()   << ","
                 << (int)color.GetGreen() << ","
                 << (int)color.GetBlue()  << endl;
    }

    fout.close();

    delete bmp;
    GdiplusShutdown(gdiplustoken);
    return 0;
}

就是这张图片
用c++获取图片上一个特定点的像素坐标
请问怎么读出这张黑色图片上的这个白色亮点的坐标?最后这部分的应该怎么改改?
用c++获取图片上一个特定点的像素坐标

30分
离了我这颗胡萝卜还真上不了席了!

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;

int main() {
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);

    wstring infilename(L"1.jpg");

    Bitmap* bmp = new Bitmap(infilename.c_str());
    UINT height = bmp->GetHeight();
    UINT width  = bmp->GetWidth();

    Color color;

    for (UINT y = 0; y < height; y++)
    for (UINT x = 0; x < width ; x++) {
            bmp->GetPixel(x, y, &color);
            if ((int)color.GetRed  ()>200
             && (int)color.GetGreen()>200
             && (int)color.GetBlue ()>200) {
               cout << x << "," << y << endl;
               goto EXIT;
            }
    }
EXIT:
    delete bmp;
    GdiplusShutdown(gdiplustoken);
    return 0;
}
输出到文件,参考#1
Quote: 引用 10 楼 zhao4zhong1 的回复:

离了我这颗胡萝卜还真上不了席了!

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;

int main() {
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);

    wstring infilename(L"1.jpg");

    Bitmap* bmp = new Bitmap(infilename.c_str());
    UINT height = bmp->GetHeight();
    UINT width  = bmp->GetWidth();

    Color color;

    for (UINT y = 0; y < height; y++)
    for (UINT x = 0; x < width ; x++) {
            bmp->GetPixel(x, y, &color);
            if ((int)color.GetRed  ()>200
             && (int)color.GetGreen()>200
             && (int)color.GetBlue ()>200) {
               cout << x << "," << y << endl;
               goto EXIT;
            }
    }
EXIT:
    delete bmp;
    GdiplusShutdown(gdiplustoken);
    return 0;
}

[/quot
非常感谢您!

颗→棵

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明用c++获取图片上一个特定点的像素坐标
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!