त्रुटि: अपर्याप्त स्मृति, छवि से पाठ। (सी#)


नमस्ते, मैं एक पाठ की एक छवि बनाना चाहता हूँ। और मैं छवि (ओसीआर) से टेक्स्ट ढूंढना चाहता हूं। लेकिन यह एक त्रुटि देता है. “अपर्याप्त स्मृति”

मेरा कोड:

सी#
private Image DetectNumbers2(Image img, Color c)
    {
        try
        {
            Bitmap bimg = new Bitmap(img);
            for (int x = 0; x < bimg.Width - 1; x++)
            {
                for (int y = 0; y < bimg.Height - 1; y++)
                {
                    Color c2 = bimg.GetPixel(x, y);
                    if (c2.R >= 240 && c2.R <= 255 && c2.G >= 45 && c2.G <= 60 && c2.B >= 45 && c2.B <= 60)
                    {
                        var img2 = bimg.Clone(new Rectangle(x, y, bimg.Height-y-1, bimg.Width-x-1), pictureBox3.Image.PixelFormat);
                        return img;
                    }
                }
            }
            return null;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return null;
        }
    }

मेरा बटन:

सी#
if (DetectNumbers2(pictureBox6.Image, GetReadColor(int.Parse(label1.Text))) != null) pictureBox11.Image = DetectNumbers2(pictureBox6.Image, GetReadColor(int.Parse(label1.Text)));

समाधान 1

मुझे दो समस्याएँ दिखती हैं, पहली:

सी#
if (DetectNumbers2(pictureBox6.Image, GetReadColor(int.Parse(label1.Text))) != null) pictureBox11.Image = DetectNumbers2(pictureBox6.Image, GetReadColor(int.Parse(label1.Text)));

OCR महंगा है, और आप जो कर रहे हैं (GetPixel का उपयोग करके) वह बहुत महंगा है, भले ही आप किसी छवि का एक टुकड़ा वापस कर रहे हों। तो फिर आप एक ही चीज़ दो बार क्यों कर रहे हैं? आप किसी छवि को शून्य निर्दिष्ट कर सकते हैं, बस यह करें:

सी#
pictureBox11.Image = DetectNumbers2(pictureBox6.Image, GetReadColor(int.Parse(label1.Text)));

और इफ स्टेटमेंट को छोड़ दें। यदि आपको उपयोगकर्ता को बताने की आवश्यकता है, तो आप बाद में जांच सकते हैं, जैसे

सी#
if (pictureBox11.Image == null)
  //Tell the user that it failed....

आगे यह पंक्ति है, हो सकता है कि आपकी समस्या कहाँ से आ रही हो:

सी#
var img2 = bimg.Clone(new Rectangle(x, y, bimg.Height-y-1, bimg.Width-x-1), pictureBox3.Image.PixelFormat);

आयतों को इस प्रकार परिभाषित किया गया है: (x, y, चौड़ाई, ऊँचाई)। आप इसे (x, y, ऊँचाई, चौड़ाई) के रूप में परिभाषित करने का प्रयास कर रहे हैं, जो गलत है।

यदि आप इसके लिए दस्तावेज़ देखें सिस्टम.ड्राइंग.बिटमैप.क्लोन()[^] आप देख सकते हैं कि यदि आयत की चौड़ाई/ऊंचाई मूल बिटमैप की सीमा से बाहर है तो यह एक आउटऑफमेमरीएक्सेप्शन देता है।

समाधान 3

The error you're encountering, "insufficient memory," typically occurs when your application consumes more memory than is available, leading to an out-of-memory exception. In your code, the issue likely arises from the `Clone` method call within the nested loops, where you're attempting to create a new bitmap for each pixel that meets the specified color criteria.

To address this issue and improve memory usage, consider the following suggestions:

1. **Reduce Image Size**: If the image is large, consider resizing it before performing OCR. This can significantly reduce memory consumption.

2. **Optimize Pixel Processing**: Instead of cloning the entire bitmap for each matching pixel, you can store the coordinates of matching pixels and process them afterward.

3. **Dispose Unused Bitmaps**: Ensure that you dispose of any bitmap objects that are no longer needed to release memory resources promptly.

Here's an updated version of your code implementing these suggestions:


private List<Point> FindMatchingPixels(Bitmap bitmap, Color color)
{
    List<Point> matchingPixels = new List<Point>();

    for (int x = 0; x < bitmap.Width; x++)
    {
        for (int y = 0; y < bitmap.Height; y++)
        {
            Color pixelColor = bitmap.GetPixel(x, y);
            if (pixelColor.R >= 240 && pixelColor.R <= 255 
                && pixelColor.G >= 45 && pixelColor.G <= 60 
                && pixelColor.B >= 45 && pixelColor.B <= 60)
            {
                matchingPixels.Add(new Point(x, y));
            }
        }
    }

    return matchingPixels;
}

private void ProcessMatchingPixels(Bitmap bitmap, List<Point> matchingPixels)
{
    foreach (Point point in matchingPixels)
    {
        // Process the matching pixels here
        // For example, extract text using OCR
    }
}

private void YourImageProcessingMethod(Image img, Color c)
{
    try
    {
        Bitmap bitmap = new Bitmap(img);
        List<Point> matchingPixels = FindMatchingPixels(bitmap, c);
        ProcessMatchingPixels(bitmap, matchingPixels);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}


In this updated code:
- The `FindMatchingPixels` method iterates through each pixel of the bitmap, identifies matching pixels, and stores their coordinates in a list.
- The `ProcessMatchingPixels` method processes the matching pixels (e.g., extracts text using OCR).
- The `YourImageProcessingMethod` method is a placeholder for your actual image processing logic, where you call the `FindMatchingPixels` and `ProcessMatchingPixels` methods.

By optimizing pixel processing and reducing unnecessary bitmap cloning, you can mitigate memory-related issues in your application.

コメント

タイトルとURLをコピーしました