Skip to content

Image convertion to GrayScale/Binary/R/G/B

Hello,

This is a small exercice of Multimedia class (INE5431). We need to implement:

1) Transform the image to GrayScale
2) Generate one image to each component color (R/G/B)
3) Transform the GrayScale image to binary

To transform RGB to GrayScale we need to use this expression:

Y = 0.3R + 0.59G + 0.11B;

And them, to convert the GrayScale to Binary

if (Y > 127) {b = 1; } else { b = 0; }

If we want just one color component, we need to get all image pixels and leave just the component that we want, or just copy the component to a new image.

In this project, we access an image and we have some functions to do what we want, convertion to GrayScale, Binary, and Component RGB. From the source image, we create a buffer image of the same size to all images and we create a for to go over all pixels and make the transformation and put it to the new buffer image.

GrayScale method, receive a Bufferedmage and convert it to GrayScale.
[code lang=”java”]
public static BufferedImage criaImagemCinza(BufferedImage imgJPEG) {
// Create a new buffer to BYTE_GRAY
BufferedImage img = new BufferedImage(imgJPEG.getWidth(), imgJPEG.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = img.getRaster();
WritableRaster rasterJPEG = imgJPEG.getRaster();
// Foreach pixel we transofrm it to Gray Scale and put it on the same image
for(int h=0;h<256;h++) for(int w=0;w<256;w++) { int[] p = new int[4]; rasterJPEG.getPixel(w, h, p); p[0] = (int) (0.3 * p[0]); p[1] = (int) (0.59 * p[1]); p[2] = (int) (0.11 * p[2]); int y = p[0] + p[1] + p[2]; raster.setSample(w,h,0,y); } return img; } [/code] Binary Image method, receive a Bufferedmage and convert it to Binary. [code lang="java"] public static BufferedImage criaImagemBinaria(BufferedImage imgJPEG) { // Create a new Binary Buffer BufferedImage img = new BufferedImage(imgJPEG.getWidth(), imgJPEG.getHeight(), BufferedImage.TYPE_BYTE_BINARY); WritableRaster raster = img.getRaster(); WritableRaster rasterPB = criaImagemCinza(imgJPEG).getRaster(); // Foreach pixel check if the new one must be white or black for(int h=0;h<256;h++) for(int w=0;w<256;w++) { int[] p = new int[1]; rasterPB.getPixel(w, h, p); if(p[0] > 127) {
raster.setSample(w, h, 0, 1);
} else {
raster.setSample(w, h, 0, 0);
}
}
return img;
}
[/code]

RGB Image method, receive a Bufferedmage and a Type (0 – red, 1 – green, 2 – blue) and convert it to RGB Component that is used as parameter.
[code lang=”java”]
public static BufferedImage criaImagemRGB(BufferedImage imgJPEG, int tipo) {
// Create a new RGB Buffer
BufferedImage img = new BufferedImage(imgJPEG.getWidth(), imgJPEG.getHeight(), BufferedImage.TYPE_INT_RGB);

WritableRaster rasterT = img.getRaster();
WritableRaster raster = imgJPEG.getRaster();
// Foreach pixel just use the component that is passed as parameter.
for(int h=0;h<256;h++) for(int w=0;w<256;w++) { int[] p = new int[4]; raster.getPixel(w,h,p); rasterT.setSample(w,h,tipo-1,p[tipo-1]); } return img; } [/code] Manipulate images to GrayScale, Binary and RGB Components

Matheus

Published injava

57 Comments

  1. يخضع النظام لرقابة ترخيص رسمي يحفظ حقوق المستخدم ويؤمّن بياناته المالية.

    تتجاوز مكتبة الكازينو 7000 لعبة تشمل السلوت والطاولات من مزودين مثل Play’n GO و Evolution.

    يمكن الرهان على مباريات الدوري المصري إلى جانب كبرى البطولات الأوروبية والعالمية.

    يكافئ نظام الولاء اللاعبين النشطين بنقاط قابلة للتحويل ومزايا حصرية في المستويات العليا.

    تتنوع خيارات الدفع بين البطاقات البنكية والمحافظ الإلكترونية والكريبتو بحد أدنى منخفض.

    888starz 888starz.

  2. 888starz’ning mobil moslashuvchanligi har bir foydalanuvchiga shaxsiy tajriba taqdim etadi.
    Bonus va aksiyalar 888starz foydalanuvchilarni jalb qilishda muhim rol o’ynaydi. Doimiy aksiyalar va promokodlar esa muntazam o’yinchilarni rag’batlantiradi.
    starz888 скачать [url=https://www.888starz-uzb10.com/apk/]https://888starz-uzb10.com/apk/[/url]
    O’yin tanlovi va provayderlar assortimenti 888starz-da keng. Demo rejim yordamida yangi o’yinlarni bepul o’rganish mumkin.
    To’lovlar va mijozlarga xizmat ko’rsatish 888starz ning ustun tomonlaridan biridir. Mijozlarning fikrlari 888starz xizmatlarini rivojlantirishda muhim ahamiyat kasb etadi.

  3. تتوفر واجهة معرّبة سهلة ضمن دعم يفوق 50 لغة لتناسب لاعبي مصر.
    يوفر قسم الكازينو أكثر من 4000 لعبة سلوت من كبرى شركات التطوير.
    يشمل الرهان الرياضي أكثر من 35 نوعًا يمتد من كرة القدم والتنس إلى الهوكي و UFC.
    يقدم 888starz مكافآت مستمرة تشمل الاسترداد النقدي والترقيات.
    يقبل الموقع البطاقات والمحافظ الإلكترونية إضافة إلى أكثر من 50 عملة مشفرة مثل BTC و USDT.
    888 starz starz888

  4. يمنح 888starz مستخدمي مصر بوابة موحّدة تضم آلاف الألعاب وعشرات الرياضات.
    تتجاوز مكتبة السلوت 4000 لعبة من أبرز مزودي الصناعة.
    starz 888 888stars
    يمنح الرهان الحي احتمالات محدّثة لحظيًا مع متابعة مباشرة للمباريات.
    يقدم قسم الرياضة عرضًا ترحيبيًا بنسبة 100% حتى 100 يورو.
    يقبل الموقع البطاقات والمحافظ مثل Neteller و Skrill إلى جانب أكثر من 50 عملة رقمية.

  5. تعمل المنصة تحت ترخيص دولي يوفر بيئة آمنة وشفافة لكل مستخدم في القاهرة.
    يبرز الموقع مجموعة 888Games الخاصة ذات النتائج السريعة.
    888 ستارز 888 ستارز
    يشمل الموقع أكثر من 35 فئة رياضية تتابع أبرز الأحداث العالمية.
    ينال لاعبو الرياضة بونص 100% يبلغ 100 يورو.
    لا يستغرق إنشاء الحساب سوى دقائق معدودة على المنصة الرسمية.

  6. The game runs on five reels and twenty fixed paylines that pay from left to right.
    Lining up the lucky sevens delivers the highest standard payouts in the game.
    20 super hot 20 super hot
    The slot has fairly high volatility, meaning wins are less frequent but can be sizeable.
    The biggest payouts come from full lines of sevens combined with the jackpot feature.
    Responsible gambling practices are recommended when playing for real money.

Leave a Reply

Your email address will not be published. Required fields are marked *