{"id":1822,"date":"2016-12-13T21:46:40","date_gmt":"2016-12-13T12:46:40","guid":{"rendered":"https:\/\/www.iss.ai\/?p=1822"},"modified":"2016-12-13T21:46:40","modified_gmt":"2016-12-13T12:46:40","slug":"iplimage%e3%82%92carchive%e3%81%a7%e4%bf%9d%e5%ad%98%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.iss.ai\/?p=1822","title":{"rendered":"IplImage\u3092CArchive\u3067\u4fdd\u5b58\u3059\u308b"},"content":{"rendered":"\t\t\t\t<p>\u3053\u306e2\u5e74\u307b\u3069\u5fd9\u3057\u3059\u304e\u3066\u4f1a\u793e\u306eHP\u3092\u307b\u3063\u305f\u3089\u304b\u3057\u306b\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001WEB\u306e\u66f4\u65b0\u304c\u306a\u3044\u4f1a\u793e\u3063\u3066\u5927\u4e08\u592b\u3060\u308d\u3046\u304b\uff1f\u3063\u3066\u3044\u3046\u6642\u4ee3\u3060\u3057\u3001\u4f55\u304b\u66f8\u304b\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u306d\u3068\u3044\u3046\u5f37\u8feb\u89b3\u5ff5\u306b\u99c6\u3089\u308c\u307e\u3057\u3066\u3001\u7d042\u5e74\u3076\u308a\u306b\u8a18\u4e8b\u3092\u66f8\u3044\u3066\u304a\u308a\u307e\u3059\u3002<br \/>\n\t\t\t\t\u4e45\u3057\u3076\u308a\u306e\u8a18\u4e8b\u306e\u5185\u5bb9\u306f\u4f55\u306b\u3059\u308b\u304b\u3068\u3044\u3046\u306e\u306f\u3001\u3053\u308c\u3082\u307e\u305f\u60a9\u307e\u3057\u3044\u3068\u3053\u308d\u3067\u3059\u304c\u3001\u4e16\u9593\u306e\u95a2\u5fc3\u306e\u3042\u308b\u5148\u7aef\u6280\u8853\u306f\u3088\u305d\u306b\u307e\u304b\u305b\u3066\u3001\u81ea\u5206\u304c\u5148\u65e5\u56f0\u3063\u305f\u3053\u3068\u3092\u8a71\u984c\u306b\u53d6\u308a\u4e0a\u3052\u3066\u4e45\u3057\u3076\u308a\u306e\u8a18\u4e8b\u6295\u7a3f\u518d\u958b\u306e1\u53f7\u3068\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\t\t\t\t<p>Windows\u306e\u30a2\u30d7\u30ea\u3092C++\u3067\u30b4\u30ea\u30b4\u30ea\u66f8\u304f\u306e\u306f\u3001\u3082\u3046\u6642\u4ee3\u9045\u308c\u304b\u3082\u3057\u308c\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u753b\u50cf\u3092\u6271\u3046\u30a2\u30d7\u30ea\u3092\u66f8\u304f\u3068\u304d\u306b\u306f\u3001\u4eca\u3082MFC\u306f\u5927\u4e8b\u306a\u9078\u629e\u3057\u3060\u3068\u601d\u3046\u306e\u3067\u3059\u3002<br \/>\n\t\t\t\t2016\u5e74\u306e\u5e74\u672b\u306b\u306a\u3063\u305f\u4eca\u65e5\u3082\u300116\u5e74\u524d\u3068\u307b\u3068\u3093\u3069\u5909\u308f\u3089\u306a\u3044MFC\u3092\u4f7f\u3063\u3066Windows\u30a2\u30d7\u30ea\u3092\u66f8\u3044\u3066\u3044\u308b\u3068\u3044\u3046\u306e\u306f\u3001\u8003\u3048\u3066\u307f\u308b\u3068\u306a\u304b\u306a\u304bMFC\u3059\u3054\u3044\u3093\u3058\u3083\u306a\u3044\u304b\u3063\u3066\u601d\u3046\u3093\u3067\u3059\u304c\u540c\u610f\u3057\u3066\u304f\u308c\u308b\u4eba\u304c\u3042\u3093\u307e\u308a\u3044\u307e\u305b\u3093\u306d\u3002<br \/>\n\t\t\t\t\u65e5\u9032\u6708\u6b69\u306e\u30bd\u30d5\u30c8\u30a6\u30a8\u30a2\u6280\u8853\u306e\u306a\u304b\u306b\u3042\u3063\u3066\u3082\u3001\u672a\u3060\u306b\u7d0420\u5e74\u524d\u3068\u307b\u307c\u540c\u3058\u4ed5\u7d44\u307f\u3092\u3064\u304b\u3063\u3066\u6700\u65b0\u306eWindows\u3067\u3082\u52d5\u4f5c\u3059\u308b\u30cf\u30a4\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30bd\u30d5\u30c8\u3092\u66f8\u3051\u308b\u306e\u3067\u3059\u304b\u3089\u79c1\u306e\u3088\u3046\u306a\u4e2d\u5e74\u306e\u30bd\u30d5\u30c8\u5c4b\u3055\u3093\u306b\u306fMFC\u306f\u624b\u653e\u305b\u307e\u305b\u3093\u3002<\/p>\n\t\t\t\t<p>MFC\u3092\u4f7f\u3063\u3066\u753b\u50cf\u51e6\u7406\u30a2\u30d7\u30ea\u3092\u66f8\u3044\u3066\u3044\u308b\u3068\u753b\u50cf\u3092\u6271\u3046IplImage\u3068\u304bcv::Mat\u3068\u304b\u3092CArchive.Serialize\u3057\u3061\u3083\u3044\u305f\u3044\u6642\u304c\u3088\u304f\u3042\u308b\u8a33\u306a\u3093\u3067\u3059\u304c\u3001IplImage->imageData\u3092\u305d\u306e\u307e\u307eWrite\u3057\u3061\u3083\u3063\u305f\u308a\u3059\u308b\u3068\u3001\u3082\u306e\u3059\u3054\u3044\u30d5\u30a1\u30a4\u30eb\u304c\u5927\u304d\u304f\u306a\u308b\u308f\u3051\u3067\u3059\u3002\u3060\u304b\u3089\u591a\u5c11\u306e\u753b\u50cf\u306e\u52a3\u5316\u306f\u59a5\u5354\u3057\u3066\u3001JPEG\u5727\u7e2e\u3057\u305f\u3046\u3048\u3067, CArchive\u306bSerialize\u3057\u305f\u3044\u306a\u3068\u601d\u3046\u3053\u3068\u306b\u306a\u308b\u3093\u3067\u3059\u304c\u3001\u3044\u3056\u3084\u3063\u3066\u307f\u3088\u3046\u304b\u3068\u304a\u3082\u3046\u3068\u3001libjpeg\u4f7f\u308f\u306a\u3044\u3068\u30c0\u30e1\u304b\u306a\u3042\u3068\u304b\u3001\u7d50\u69cb\u3081\u3093\u3069\u304f\u3055\u305d\u3046\u306a\u611f\u3058\u306b\u306a\u3063\u3066\u304f\u308b\u3093\u3067\u3059\u306d\u3002<br \/>\n\t\t\t\t\u624b\u8efd\u306b\u3060\u308c\u304b\u304c\u4f5c\u3063\u3066\u304f\u308c\u305fIplImage\u3092CArchive\u3057\u3066\u304f\u308c\u308b\u30b3\u30fc\u30c9\u304c\u306a\u3044\u304b\u306a\u3042\u3068\u63a2\u3057\u3066\u307f\u305f\u308a\u3057\u305f\u306e\u3067\u3059\u304c\u3001\u4eca\u306e\u6642\u4ee3\u306bMFC\u3092\u4f7f\u3063\u3066\u308b\u4eba\u3063\u3066\u3042\u3093\u307e\u308a\u3044\u306a\u3044\u307f\u305f\u3044\u3067\u3001MFC and IplImage and CArchive\u306a\u3069\u3068\u3044\u3046\u60c5\u5831\u306f\u3001Google\u5148\u751f\u3082\u30ba\u30d0\u30ea\u306a\u7b54\u3048\u3092\u6559\u3048\u3066\u304f\u308c\u307e\u305b\u3093\u3002\u4ed5\u65b9\u306a\u3057\u306b\u3001\u307b\u304b\u306e\u65ad\u7247\u7684\u306a\u60c5\u5831\u3092\u3064\u306a\u304e\u5408\u308f\u305b\u3066\u3001Gdiplus\u306e\u6a5f\u80fd\u3092\u4f7f\u3063\u3066 IplImage\u3092CArchive\u306b\u4fdd\u7ba1\u3059\u308b\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u306e\u3067\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\t\t\t\t<p>IplImage\u3092CArchive\u306b\u4fdd\u5b58\u3059\u308b\u95a2\u6570\u304cwrite_iplimage_with_jpeg()\u3067\u3059\u3002<br \/>\n\t\t\t\tJpeg\u5727\u7e2e\u5f8c\u306e\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306e\u30e1\u30e2\u30ea\u30fc\u3092GlobalAlloc\u3067\u78ba\u4fdd\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u751f\u753b\u50cf\u30b5\u30a4\u30ba\u3088\u308a\u306f\u5c0f\u3055\u304f\u306a\u308b\u3060\u308d\u3046\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u751f\u753b\u50cf\u30b5\u30a4\u30ba\u4e38\u3054\u3068\u78ba\u4fdd\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\t\t\t\t\u305d\u306e\u5f8c\u306bGdiplus\u306b\u5727\u7e2e\u3057\u3066\u3082\u3089\u3046\u305f\u3081\u3001\u30e1\u30e2\u30ea\u30fc\u30d6\u30ed\u30c3\u30af\u304b\u3089IStrem\u30cf\u30f3\u30c9\u30eb\u3092\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u3002GetEncoderClsid()\u306f\u3001MSDN\u306e\u30b5\u30f3\u30d7\u30eb\u304b\u3089\u9802\u304d\u307e\u3057\u305f\u3002<br \/>\n\t\t\t\tIplImage2GdiPlusBitmap()\u306f\u3001IplImage\u3092Gdiplus\u306eBitmap\u306b\u5909\u63db\u3059\u308b\u81ea\u4f5c\u95a2\u6570\u3067\u3059\u3002&#8221;image\/jpeg&#8221;\u3092\u3001&#8221;image\/png&#8221;\u306b\u3057\u305f\u3089PNG\u5f62\u5f0f\u3067Serialize\u3067\u304d\u307e\u3059\u3002<br \/>\n\t\t\t\tIplImage2GdiPlusBitmap()\u3082\u8a18\u4e8b\u306e\u5f8c\u534a\u306b\u8cbc\u308a\u4ed8\u3051\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n\t\t\t\t<pre>\r\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\t\/\/IplImage\u3092JPEG\u5727\u7e2e\u3057\u3066CArchive\u306b\u4fdd\u5b58\u3059\u308b\r\n\tvoid write_iplimage_with_jpeg(IplImage* ipl, CArchive&amp; ar, int quality)\r\n\t{\r\n\t\tint w = ipl-&gt;width, h = ipl-&gt;height;\r\n\t\tHGLOBAL hJpegBuffWrite = ::GlobalAlloc(GMEM_MOVEABLE, w * h * 3);\r\n\t\tBYTE * pJpegBuffWrite = (BYTE*)::GlobalLock(hJpegBuffWrite);\r\n\t\tIStream*isImageWrite;\r\n\t\tCreateStreamOnHGlobal(hJpegBuffWrite, TRUE, &amp;isImageWrite);\/* \u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u304b\u3089IStream\u3092\u4f5c\u6210 *\/\r\n\t\tCLSID jpgClsid;\r\n\t\tGetEncoderClsid(L&quot;image\/jpeg&quot;, &amp;jpgClsid);\r\n\r\n\t\tULARGE_INTEGER pos;\r\n\t\tLARGE_INTEGER dis;\r\n\t\tdis.QuadPart = 0;\r\n\r\n\t\tBitmap* pBmp = IplImage2GdiPlusBitmap(ipl);\r\n\r\n\t\tGdiplus::EncoderParameters p;\r\n\t\tp.Count = 1;\r\n\t\tp.Parameter&#x5B;0].Guid = EncoderQuality;\r\n\t\tp.Parameter&#x5B;0].Type = EncoderParameterValueTypeLong;\r\n\t\tp.Parameter&#x5B;0].NumberOfValues = 1;\r\n\t\tp.Parameter&#x5B;0].Value = &amp;quality;\r\n\r\n\t\tpBmp-&gt;Save(isImageWrite, &amp;jpgClsid, &amp;p);\r\n\t\tdelete pBmp;\r\n\r\n\t\tisImageWrite-&gt;Seek(dis, STREAM_SEEK_CUR, &amp;pos);\r\n\t\tTRACE(&quot;after seek=%d\\n&quot;, pos.QuadPart);\r\n\r\n\t\tBYTE* pJpegBuff = (BYTE*)::GlobalLock(hJpegBuffWrite);\r\n\t\tSIZE_T jpeg_length = (SIZE_T)pos.QuadPart;\r\n\r\n\t\tar &lt;&lt; jpeg_length;\r\n\t\tar.Write(pJpegBuff, jpeg_length);\r\n\r\n\t\tisImageWrite-&gt;Release();\r\n\r\n\t\t::GlobalUnlock(hJpegBuffWrite);\r\n\t\t::GlobalFree(hJpegBuffWrite);\r\n\t}\r\n<\/pre>\n\t\t\t\t<p>\u7d9a\u3044\u3066\u3001CArchive\u304b\u3089IplImage\u3092\u8aad\u307f\u51fa\u3059\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n\t\t\t\t<pre>\r\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\t\/\/CArchive\u306b\u4fdd\u5b58\u3055\u308c\u305fJPEG\u5727\u7e2e\u753b\u50cf\u3092IplImage\u306b\u8aad\u307f\u51fa\u3059\r\n\tIplImage* read_iplimage_with_jpeg(CArchive&amp; ar)\r\n\t{\r\n\t\t\/\/ar &gt;&gt; \r\n\t\tSIZE_T jpeg_length;\r\n\t\tar &gt;&gt; jpeg_length;\r\n\t\tHGLOBAL hJpegBuff = ::GlobalAlloc(GMEM_MOVEABLE, jpeg_length);\r\n\t\tBYTE * pJpegBuff = (BYTE*)::GlobalLock(hJpegBuff);\r\n\t\tar.Read(pJpegBuff, jpeg_length);\/\/JPEG\u30c7\u30fc\u30bf\u306e\u8aad\u307f\u8fbc\u307f\r\n\t\t::GlobalUnlock(hJpegBuff);\r\n\t\tIStream* isImage;\r\n\t\tCreateStreamOnHGlobal(hJpegBuff, TRUE, &amp;isImage);\/* \u30e1\u30e2\u30ea\u30d6\u30ed\u30c3\u30af\u304b\u3089IStream\u3092\u4f5c\u6210 *\/\r\n\t\tGdiplus::Bitmap bmp(isImage);\r\n\r\n\t\tIplImage* new_image = GdiPlusBitmap2IplImage(&amp;bmp);\r\n\t\tTRACE(&quot;(%d,%d)\\n&quot;, bmp.GetWidth(), bmp.GetHeight());\r\n\r\n\t\t::GlobalFree(hJpegBuff);\r\n\t\treturn new_image;\r\n\t}\r\n<\/pre>\n\t\t\t\t<p>IplImage\u304b\u3089Gdiplus\u306eBitmap\u306b\u5909\u63db\u3059\u308b\u95a2\u6570\u3068\u305d\u306e\u9006\u306e\u95a2\u6570\u3067\u3059\u3002GdiPlusBitmap2IplImage()\u306e\u623b\u308a\u5024\u306eIplImage*\u306f\u3001\u4f7f\u7528\u5f8c\u306bRelease\u3059\u308b\u306e\u3092\u5fd8\u308c\u3066\u306f\u3044\u3051\u307e\u305b\u3093\u3002<\/p>\n\t\t\t\t<pre>\r\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tGdiplus::Bitmap*  IplImage2GdiPlusBitmap(IplImage* pIplImg)\r\n\t{\r\n\t\tif (!pIplImg)\r\n\t\t\treturn NULL;\r\n\r\n\t\tGdiplus::Bitmap *pBitmap = new Gdiplus::Bitmap(pIplImg-&gt;width, pIplImg-&gt;height, PixelFormat24bppRGB);\r\n\t\tif (!pBitmap)\r\n\t\t\treturn NULL;\r\n\r\n\t\tGdiplus::BitmapData bmpData;\r\n\t\tGdiplus::Rect rect(0, 0, pIplImg-&gt;width, pIplImg-&gt;height);\r\n\t\tpBitmap-&gt;LockBits(&amp;rect, Gdiplus::ImageLockModeWrite, PixelFormat24bppRGB, &amp;bmpData);\r\n\t\tBYTE *pByte = (BYTE*)bmpData.Scan0;\r\n\r\n\t\tif (pIplImg-&gt;widthStep == bmpData.Stride) \/\/likely  \r\n\t\t\tmemcpy(bmpData.Scan0, pIplImg-&gt;imageDataOrigin, pIplImg-&gt;imageSize);\r\n\r\n\t\tpBitmap-&gt;UnlockBits(&amp;bmpData);\r\n\t\treturn pBitmap;\r\n\t}\r\n\r\n\tIplImage* GdiPlusBitmap2IplImage(Gdiplus::Bitmap* bmp)\r\n\t{\r\n\t\tauto format = bmp-&gt;GetPixelFormat();\r\n\t\tif (format != PixelFormat24bppRGB)\r\n\t\t\treturn nullptr;\r\n\r\n\t\tGdiplus::Rect rcLock(0, 0, bmp-&gt;GetWidth(), bmp-&gt;GetHeight());\r\n\t\tGdiplus::BitmapData bmpData;\r\n\r\n\t\tbmp-&gt;LockBits(&amp;rcLock, Gdiplus::ImageLockModeRead, format, &amp;bmpData);\r\n\r\n\t\tint buffSz = bmpData.Stride * bmpData.Height;\r\n\t\tint depth = 8, channel = 3;\r\n\t\tIplImage* cvImage = cvCreateImage(cvSize(rcLock.Width, rcLock.Height), depth, channel);\r\n\t\tconst unsigned char* src = static_cast&lt;unsigned char*&gt;(bmpData.Scan0);\r\n\t\tstd::copy(src, src + buffSz, cvImage-&gt;imageData);\r\n\r\n\t\tbmp-&gt;UnlockBits(&amp;bmpData);\r\n\t\tif (bmpData.Stride&lt;0)\r\n\t\t\tcvFlip(cvImage, cvImage);\r\n\r\n\t\treturn cvImage;\r\n\t}\r\n\r\n<\/pre>\n\t\t\t\t<p>MSDN\u306e\u30b5\u30f3\u30d7\u30eb\u304b\u3089\u5f15\u3063\u5f35\u3063\u3066\u304d\u305f\u95a2\u6570\u3067\u3059\u3002<\/p>\n\t\t\t\t<pre>\r\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tstatic int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)\r\n\t{\r\n\t\tUINT  num = 0;          \/\/ number of image encoders\r\n\t\tUINT  size = 0;         \/\/ size of the image encoder array in bytes\r\n\r\n\t\tImageCodecInfo* pImageCodecInfo = NULL;\r\n\r\n\t\tGetImageEncodersSize(&amp;num, &amp;size);\r\n\t\tif (size == 0)\r\n\t\t\treturn -1;  \/\/ Failure\r\n\r\n\t\tpImageCodecInfo = (ImageCodecInfo*)(malloc(size));\r\n\t\tif (pImageCodecInfo == NULL)\r\n\t\t\treturn -1;  \/\/ Failure\r\n\r\n\t\tGetImageEncoders(num, size, pImageCodecInfo);\r\n\r\n\t\tfor (UINT j = 0; j &lt; num; ++j)\r\n\t\t{\r\n\t\t\tif (wcscmp(pImageCodecInfo&#x5B;j].MimeType, format) == 0)\r\n\t\t\t{\r\n\t\t\t\t*pClsid = pImageCodecInfo&#x5B;j].Clsid;\r\n\t\t\t\tfree(pImageCodecInfo);\r\n\t\t\t\treturn j;  \/\/ Success\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tfree(pImageCodecInfo);\r\n\t\treturn -1;  \/\/ Failure\r\n\t}\r\n<\/pre>\n\t\t\t\t<p>T.Kamata<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IplImage\u3092CArchive\u3067\u8aad\u307f\u66f8\u304d\u3059\u308b\u305f\u3081\u306e\u30b3\u30fc\u30c9\u306e\u7d39\u4ecb<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,3],"tags":[],"class_list":["post-1822","post","type-post","status-publish","format-standard","hentry","category-soft","category-tech"],"_links":{"self":[{"href":"https:\/\/www.iss.ai\/index.php?rest_route=\/wp\/v2\/posts\/1822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.iss.ai\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iss.ai\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iss.ai\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iss.ai\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1822"}],"version-history":[{"count":5,"href":"https:\/\/www.iss.ai\/index.php?rest_route=\/wp\/v2\/posts\/1822\/revisions"}],"predecessor-version":[{"id":1837,"href":"https:\/\/www.iss.ai\/index.php?rest_route=\/wp\/v2\/posts\/1822\/revisions\/1837"}],"wp:attachment":[{"href":"https:\/\/www.iss.ai\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iss.ai\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iss.ai\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}