找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 845|回复: 1

[每日一码] 如何查询raster image的像素值

[复制链接]

已领礼包: 13个

财富等级: 恭喜发财

发表于 2017-3-3 23:13:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Issue
How do I find out the pixel value in a raster image inserted into an AutoCAD
drawing.

Solution
To convert a coordinate in the world coordinates system, you can use the AcDbRasterImage::getPixelToModelTransform() function. Then you will need to use the Atil SDK to access the individual pixels of the image.

Following code demonstrates how to do it:


  1. void AsdkGetPixelValue()
  2. {
  3. ads_name name;
  4. ads_point pt;
  5. Acad::ErrorStatus es;

  6. if (acedEntSel(L"\nSelect a Raster Image : ", name, pt) != RTNORM)
  7. {
  8.   return;
  9. }

  10. AcDbObjectId objId;
  11. acdbGetObjectId(objId, name);


  12. AcDbEntity * pEnt;
  13. es = acdbOpenObject(pEnt, objId, AcDb::kForRead);
  14. AcDbRasterImage *pRaster = AcDbRasterImage::cast(pEnt);
  15. if (pRaster == NULL)
  16. {
  17. pEnt->close();
  18.   return;
  19. }


  20. // Get pixel value
  21. AcGeVector2d imgSize = pRaster->imageSize();
  22. AcGeMatrix3d mat;
  23. es = pRaster->getPixelToModelTransform(mat);
  24. AcGePoint3d modpt;

  25. if (acedGetPoint(NULL, L"\nSelect point in image : ", asDblArray(modpt)) != RTNORM)
  26. {
  27.   return;
  28. }


  29. AcGePoint3d pixpt(0,0,0);
  30. pixpt = mat.inverse() * modpt;


  31. int x = (int)pixpt[0];
  32. int y = (int)pixpt[1];


  33. if ((x > imgSize.x) || (x < 0) || (y > imgSize.y) || (y < 0))
  34. {
  35. acutPrintf(L"\n*** This point is not within the image ***\n");
  36. pRaster->close();
  37.   return;
  38. }
  39. else
  40. {
  41. acutPrintf(L"\nPixel Selected: (%d, %d)", x, y);
  42. }


  43. // Get a copy of the image from the rastier image def
  44. AcDbRasterImageDef * pDef;

  45. es = acdbOpenObject(pDef, pRaster->imageDefId(), AcDb::kForWrite);
  46. Atil::Image* pImg = pDef->imageCopy();


  47. pRaster->close();
  48. pDef->close();



  49. // Find out bits per pixel of image
  50. Atil::ImageContext* imgContext = pImg->createContext(Atil::ImageContext::kRead, pImg->size(), Atil::Offset(0,0));

  51. Atil::DataModelAttributes::BitsPerPixel bpp = pImg->dataModel().bitsPerPixel();

  52. // Show pixel value
  53. switch (bpp)
  54. {
  55. case Atil::DataModelAttributes::BitsPerPixel::k1:
  56. acutPrintf(L"\nPixel value = %u\n", imgContext->get1(x,y));
  57.   break;
  58. case Atil::DataModelAttributes::BitsPerPixel::k8:
  59. acutPrintf(L"\nPixel value = %u\n", imgContext->get8(x,y));
  60.   break;
  61. case Atil::DataModelAttributes::BitsPerPixel::k16:
  62. acutPrintf(L"\nPixel value = %u\n", imgContext->get16(x,y));
  63.   break;
  64. case Atil::DataModelAttributes::BitsPerPixel::k32:
  65. acutPrintf(L"\nPixel value = %u\n", imgContext->get32(x,y));
  66.   break;
  67. case Atil::DataModelAttributes::BitsPerPixel::k64:
  68. acutPrintf(L"\nPixel value = %u\n", imgContext->get64(x,y));
  69.   break;
  70. default:
  71. acutPrintf(L"\n*** This color scale is not supported. ***\n");
  72. }


  73. //Clean up
  74. delete imgContext;
  75. delete pImg;

  76. return;
  77. }



Please note that this code incorporates only minimal error checking.

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-9-24 18:26 , Processed in 0.372757 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表