<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Bild serialisieren]]></title><description><![CDATA[<p>Ich würde gerne meine Klasse BildDaten mit Hilfe von boost serialisieren.</p>
<pre><code>#ifndef BildDaten_hpp
#define BildDaten_hpp

#include &quot;../Daten.hpp&quot;
#include &lt;opencv/cv.h&gt;
#include &lt;opencv/highgui.h&gt;
#include &lt;string&gt;

//! Dies ist die Datenklasse vom CameraModul. Damit versendet das CameraModul Bilder. Dabei wird das Bild in zwei Formaten weitergegeben. Im IplImage und im Mat Format.
class BildDaten : public Daten
{
  public:
    BildDaten();
    BildDaten(cv::Mat*, IplImage*);
    virtual ~BildDaten();
    cv::Mat* leseMat() const;
    IplImage* leseImg() const;

  private:
    friend class boost::serialization::access; 

    template &lt;typename Archive&gt;
    void save(Archive &amp;ar, const unsigned int version)
    {
      ar &lt;&lt; boost::serialization::base_object&lt;Daten&gt;(*this); 

      // Wir extrahieren die Bildeigenschaften.
      int height = Img-&gt;height;
      int width = Img-&gt;width;
      int nChannels = Img-&gt;nChannels;
      int depth = Img-&gt;depth;
      int size = Img-&gt;imageSize;
      char* data = Img-&gt;imageData;

      ar &lt;&lt; width &lt;&lt; height &lt;&lt; depth &lt;&lt; nChannels &lt;&lt; size;
      ar &lt;&lt; boost::serialization::binary_object(data, size);
    };

    template &lt;class Archive&gt;
    void load(Archive &amp;ar, const unsigned int version)
    {
      int height, width, nChannels, depth, size;
      char* data;

      ar &gt;&gt; width &gt;&gt; height &gt;&gt; depth &gt;&gt; nChannels &gt;&gt; size;

      Img = cvCreateImage(cvSize(width,height), depth, nChannels);

      ar &gt;&gt; boost::serialization::binary_object(Img-&gt;imageData, size);
    }

    cv::Mat* Mat;
    IplImage* Img;
};
#endif
</code></pre>
<p>Das klappt eigentlich wunderbar. Der Kode lässt sich kompilieren und ausführen. Ein binäres Archiv wird geöffnet und ein Bild darin gespeichert (denke ich, denn es ist ein Bild und 70KB groß).<br />
Will ich es aber wieder aus dem Archiv holen bekomme ich eine Nullpointerexception.</p>
<pre><code>BildDaten* B;
  Bildarchiv &gt;&gt; B;
  IplImage* Img;
  Img = B-&gt;leseImg();
  int height = Img-&gt;height;
</code></pre>
<p>Ich vermute stark es liegt an der Zuweise zum Zeiger Img!? Die Zuweisung ist doch nur innerhalb der Methode load() gültig, oder?<br />
Hat mir jemand einen Tipp oder weiß woran es liegt?</p>
<p>Gruß fabske</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/269652/bild-serialisieren</link><generator>RSS for Node</generator><lastBuildDate>Mon, 31 Aug 2026 01:22:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/269652.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 29 Jun 2010 06:27:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Bild serialisieren on Tue, 29 Jun 2010 06:27:26 GMT]]></title><description><![CDATA[<p>Ich würde gerne meine Klasse BildDaten mit Hilfe von boost serialisieren.</p>
<pre><code>#ifndef BildDaten_hpp
#define BildDaten_hpp

#include &quot;../Daten.hpp&quot;
#include &lt;opencv/cv.h&gt;
#include &lt;opencv/highgui.h&gt;
#include &lt;string&gt;

//! Dies ist die Datenklasse vom CameraModul. Damit versendet das CameraModul Bilder. Dabei wird das Bild in zwei Formaten weitergegeben. Im IplImage und im Mat Format.
class BildDaten : public Daten
{
  public:
    BildDaten();
    BildDaten(cv::Mat*, IplImage*);
    virtual ~BildDaten();
    cv::Mat* leseMat() const;
    IplImage* leseImg() const;

  private:
    friend class boost::serialization::access; 

    template &lt;typename Archive&gt;
    void save(Archive &amp;ar, const unsigned int version)
    {
      ar &lt;&lt; boost::serialization::base_object&lt;Daten&gt;(*this); 

      // Wir extrahieren die Bildeigenschaften.
      int height = Img-&gt;height;
      int width = Img-&gt;width;
      int nChannels = Img-&gt;nChannels;
      int depth = Img-&gt;depth;
      int size = Img-&gt;imageSize;
      char* data = Img-&gt;imageData;

      ar &lt;&lt; width &lt;&lt; height &lt;&lt; depth &lt;&lt; nChannels &lt;&lt; size;
      ar &lt;&lt; boost::serialization::binary_object(data, size);
    };

    template &lt;class Archive&gt;
    void load(Archive &amp;ar, const unsigned int version)
    {
      int height, width, nChannels, depth, size;
      char* data;

      ar &gt;&gt; width &gt;&gt; height &gt;&gt; depth &gt;&gt; nChannels &gt;&gt; size;

      Img = cvCreateImage(cvSize(width,height), depth, nChannels);

      ar &gt;&gt; boost::serialization::binary_object(Img-&gt;imageData, size);
    }

    cv::Mat* Mat;
    IplImage* Img;
};
#endif
</code></pre>
<p>Das klappt eigentlich wunderbar. Der Kode lässt sich kompilieren und ausführen. Ein binäres Archiv wird geöffnet und ein Bild darin gespeichert (denke ich, denn es ist ein Bild und 70KB groß).<br />
Will ich es aber wieder aus dem Archiv holen bekomme ich eine Nullpointerexception.</p>
<pre><code>BildDaten* B;
  Bildarchiv &gt;&gt; B;
  IplImage* Img;
  Img = B-&gt;leseImg();
  int height = Img-&gt;height;
</code></pre>
<p>Ich vermute stark es liegt an der Zuweise zum Zeiger Img!? Die Zuweisung ist doch nur innerhalb der Methode load() gültig, oder?<br />
Hat mir jemand einen Tipp oder weiß woran es liegt?</p>
<p>Gruß fabske</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1918700</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1918700</guid><dc:creator><![CDATA[fabske]]></dc:creator><pubDate>Tue, 29 Jun 2010 06:27:26 GMT</pubDate></item><item><title><![CDATA[Reply to Bild serialisieren on Tue, 29 Jun 2010 10:16:04 GMT]]></title><description><![CDATA[<p>Was genau macht denn die Anweisung &quot;Bildarchiv &gt;&gt; B;&quot;?</p>
<p>Soll der Streaming-Operator wirklich auf einem Zeiger von BildDaten arbeiten und nicht direkt auf einem Bilddaten-Objekt?</p>
<p>Schau doch einfach mit dem Debugger nach, welche Variable konkret falsch gesetzt ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1918801</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1918801</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Tue, 29 Jun 2010 10:16:04 GMT</pubDate></item><item><title><![CDATA[Reply to Bild serialisieren on Tue, 29 Jun 2010 10:32:50 GMT]]></title><description><![CDATA[<p>Anscheinend gibt es Probleme mit der Speicherverwaltung. So sieht mein Programm aus, welches das Bild ins Archiv serialisiert:</p>
<pre><code>BD ist ein Zeiger auf ein Objekt vom Typ BildDaten() und bereits vorhanden!

    boost::archive::binary_oarchive Bildarchiv(&quot;Bilder&quot;);

    // im 2. Takt schieben wir das Bild rein.
    if(Takt==2)
    {
      // vorher geben wir mal die Werte aus
      IplImage* Img;
      Img = BD-&gt;leseImg();

      int height = Img-&gt;height;
      int width = Img-&gt;width;
      int nChannels = Img-&gt;nChannels;
      int depth = Img-&gt;depth;
      int size = Img-&gt;imageSize;
      char* data = Img-&gt;imageData;

      std::stringstream ausgabe;
      ausgabe &lt;&lt; &quot;width: &quot; &lt;&lt; width &lt;&lt; &quot; height: &quot; &lt;&lt; height &lt;&lt; &quot; depth: &quot;&lt;&lt; depth &lt;&lt; &quot; channels: &quot; &lt;&lt; nChannels &lt;&lt; &quot; size: &quot; &lt;&lt; size &lt;&lt; std::endl;
      cout &lt;&lt; ausgabe.str();

      Bildarchiv &lt;&lt; BD;
    }
</code></pre>
<p>Soweit so gut. Es entsteht eine Binärdatei &quot;Bilder&quot; mit aber nur 64 Byte! Kann das sein? Funktioniert ar &lt;&lt; boost::serialization::binary_object(data, size); nicht?</p>
<p>Nun will ich das Bild in einem anderen Programm wieder raus holen:</p>
<pre><code>BildDaten* B = new BildDaten();

  Bildarchiv &gt;&gt; B;

  std::stringstream ausgabe1;
  ausgabe1 &lt;&lt; &quot;Takt: &quot; &lt;&lt;  B-&gt;leseTakt() &lt;&lt; &quot;  Ersteller: &quot; &lt;&lt; B-&gt;leseErsteller() &lt;&lt; &quot;  Erstellungszeit: &quot; &lt;&lt; B-&gt;leseErstellungszeit() &lt;&lt; std::endl;
  HIRN::schreiben(ausgabe1.str());

  IplImage* Img = B-&gt;leseImg();

      std::cout &lt;&lt; &quot;AAA\n&quot;;

      int height = Img-&gt;height;
      int width = Img-&gt;width;
      int nChannels = Img-&gt;nChannels;
      int depth = Img-&gt;depth;
      int size = Img-&gt;imageSize;
      //char* data = Img-&gt;imageData;

      std::stringstream ausgabe;
      ausgabe &lt;&lt; &quot;width: &quot; &lt;&lt; width &lt;&lt; &quot; height: &quot; &lt;&lt; height &lt;&lt; &quot; depth: &quot;&lt;&lt; depth &lt;&lt; &quot; channels: &quot; &lt;&lt; nChannels &lt;&lt; &quot; size: &quot; &lt;&lt; size &lt;&lt; std::endl;
      std::cout &lt;&lt; ausgabe.str();

      std::cout &lt;&lt; &quot;BBB\n&quot;;
</code></pre>
<p>Ich erstelle eine Zeiger auf BildDaten() B und erzeuge ein Objekt, ansonsten hab ich ja keinen Speicher allokiert. Aber der Zeiger in B auf ein IplImage hat ja keinen Speicher allokiert! Ist das ein Problem?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1918811</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1918811</guid><dc:creator><![CDATA[fabske]]></dc:creator><pubDate>Tue, 29 Jun 2010 10:32:50 GMT</pubDate></item><item><title><![CDATA[Reply to Bild serialisieren on Tue, 29 Jun 2010 11:58:57 GMT]]></title><description><![CDATA[<p>Du musst vor dem deserialisieren kein Objekt anlegen. Das macht <code>boost.serialization</code> bei Serialisierung durch Pointer selbst. Freigeben musst Du es dann am Ende aber wieder.<br />
Hast Du mal geguckt, welchen Wert der <code>size</code> -Parameter beim Serialisieren hat? So ca. 64 vllt.?</p>
<p>Lies evtl. auch mal <a href="http://www.boost.org/doc/libs/1_43_0/libs/serialization/doc/serialization.html#pointeroperators" rel="nofollow">das hier</a> und auch die anderen Themen zur Pointerserialisierung in der Doku...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1918870</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1918870</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Tue, 29 Jun 2010 11:58:57 GMT</pubDate></item><item><title><![CDATA[Reply to Bild serialisieren on Tue, 29 Jun 2010 13:15:29 GMT]]></title><description><![CDATA[<p>In der Tat! sizeof(data) hat den Wert 4! (VOR dem Serialisieren, also das ganz normale Bild) Aber warum??</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1918931</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1918931</guid><dc:creator><![CDATA[fabske]]></dc:creator><pubDate>Tue, 29 Jun 2010 13:15:29 GMT</pubDate></item><item><title><![CDATA[Reply to Bild serialisieren on Tue, 29 Jun 2010 13:41:57 GMT]]></title><description><![CDATA[<p>fabske schrieb:</p>
<blockquote>
<p>In der Tat! sizeof(data) hat den Wert 4! (VOR dem Serialisieren, also das ganz normale Bild) Aber warum??</p>
</blockquote>
<p>Bekommt bei <code>int size = Img-&gt;imageSize</code> das <code>imageSize</code> seine Größe über <code>sizeof(data)</code> zugewiesen?<br />
Dann liegt es daran, dass <code>data</code> ein Pointer ist, und <code>sizeof(T*)</code> liefert Dir <em>die Größe eines Pointers</em> auf Deiner Plattform (also wohl 4).</p>
<p>Wenn Du mit rohen Array-Objekten auf dem Heap hantierst, musst Du die Größe immer mitspeichern. Oder Du benutzt <code>std::vector</code> . Der macht das für Dich automatisch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1918951</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1918951</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Tue, 29 Jun 2010 13:41:57 GMT</pubDate></item><item><title><![CDATA[Reply to Bild serialisieren on Tue, 29 Jun 2010 16:20:57 GMT]]></title><description><![CDATA[<p>Eigentlich ist es so in Ordnung und wie du gesagt hast!</p>
<pre><code>int size = Img-&gt;imageSize;
      char* data = Img-&gt;imageData;

      std::stringstream ausgabe;
      ausgabe &lt;&lt; &quot;size: &quot; &lt;&lt; size &lt;&lt; &quot; sizeof: &quot; &lt;&lt; sizeof(Img-&gt;imageData) &lt;&lt; std::endl;
</code></pre>
<p>Heraus kommt: size: 1082880 sizeof: 4</p>
<p>Aber warum zum Henker serialisiert er denn dann nicht??</p>
<p>ar &lt;&lt; boost::serialization::make_binary_object(data, size);</p>
<p>muss doch funktionieren?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1919001</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1919001</guid><dc:creator><![CDATA[fabske]]></dc:creator><pubDate>Tue, 29 Jun 2010 16:20:57 GMT</pubDate></item></channel></rss>