なんとなく、ふわっと・・

写真と画像処理関係とひとりごとをなんとなく書き溜めていきたい

Crop と SmoothEdge

2008-03-24 19:02:31 | C#(Graphics)

旧ブログからのコード移植、その3。

Crop は、Mask をつかって、指定色領域を指定の色許容度で透明にする。
SmoothEdge は、Crop された画像のエッジのアルファ値を傾斜をかけて滑らかにする。



全コードをしめす。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using ImageUtils;

namespace CropSmoothEdge
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static bool Crop(ref Bitmap bmp, Bitmap mask, Color maskColor,
                                                      int tolerance)
        {
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if (mask.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            if ((tolerance < 0) | (tolerance > 100))
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            if ((w != mask.Width) | (h != mask.Height)) return false;

            byte redSmall = ImgUtils.AdjustByte(maskColor.R - tolerance);
            byte redLarge = ImgUtils.AdjustByte(maskColor.R + tolerance);
            byte greenSmall = ImgUtils.AdjustByte(maskColor.G - tolerance);
            byte greenLarge = ImgUtils.AdjustByte(maskColor.G + tolerance);
            byte blueSmall = ImgUtils.AdjustByte(maskColor.B - tolerance);
            byte blueLarge = ImgUtils.AdjustByte(maskColor.B + tolerance);

            Bitmap tmp = new Bitmap(w, h, PixelFormat.Format32bppArgb);

            Graphics g = Graphics.FromImage(tmp);
            g.DrawImageUnscaled(bmp, 0, 0);
            g.Dispose();

            BmpProc24 src = new BmpProc24(mask);
            BmpProc32 dst = new BmpProc32(tmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    src.SetXY(x, y);
                    if ((src.R < redSmall) | (src.R > redLarge) |
                        (src.G < greenSmall) | (src.G > greenLarge) |
                        (src.B < blueSmall) | (src.B > blueLarge)) continue;

                    dst[x, y, eRGB.a] = 0;
                }

            ImgUtils.CallDispose(dst, src, bmp);

            bmp = tmp;

            return true;
        }

        public static bool SmoothEdge(ref Bitmap bmp, int zone)
        {
            if (bmp.PixelFormat != PixelFormat.Format32bppArgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            Bitmap tmp = bmp.Clone() as Bitmap;

            ImgUtils.Blur32(ref bmp, zone);

            BmpProc32 src = new BmpProc32(tmp);
            BmpProc32 dst = new BmpProc32(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    src.SetXY(x, y);
                    dst.SetXY(x, y);

                    if (dst.A == 0) continue;

                    if (src.A == dst.A)
                    {
                        dst.R = src.R;
                        dst.G = src.G;
                        dst.B = src.B;
                    }
                }

            ImgUtils.CallDispose(dst, src, tmp);

            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(@"c:\Home\ImgWork\CropSample.png");

            Bitmap mask = new Bitmap(@"c:\Home\ImgWork\CropMask.png");

            Bitmap haikei = new Bitmap(@"c:\Home\ImgWork\CropBack.png");

            Color maskColor = mask.GetPixel(50, 50);

            Crop(ref bmp, mask, maskColor, 40);            

            Graphics g = Graphics.FromImage(haikei);
            g.DrawImage(bmp, -160, 0, bmp.Width, bmp.Height);
            SmoothEdge(ref bmp, 2);
            g.DrawImage(bmp, 230, 0, bmp.Width, bmp.Height);
            g.Dispose();

            g = this.CreateGraphics();
            g.DrawImageUnscaled(haikei, 5, 35);
            Clipboard.SetImage(haikei);
            g.Dispose();

            haikei.Dispose();
            mask.Dispose();
            bmp.Dispose();
        }
    }
}


CropSample



CropMask



CropBack



Comments (4)

crane #1

2008-03-24 00:27:13 | 写真


吾妻1、つくば市



[Sigma 17-70mmF2.8-4.5DCMacro]
[HDR] from 9 jpeg shots of -3.7, -2.0, -1.7, -0.7, 0.0, 0.3, 1.3, 2.0, 3.3 EV




Comments (6)

too much pink

2008-03-24 00:18:33 | 写真

Dahlia #10

茨城県フラワーパーク、石岡市




[DFA Macro 100mmF2.8]
[HDR]




Comment

S

2008-03-24 00:08:03 | rinkaku



Comment

乙戸沼公園 #1

2008-03-23 00:23:46 | 写真


乙戸沼公園、土浦市





[Sigma 17-70mmF2.8-4.5DCMacro]
[HDR]


Comments (2)

S

2008-03-23 00:11:29 | rinkaku






Comment

SetAlpha32

2008-03-23 00:06:11 | C#(Graphics)

旧ブログからのコード移植、その2。

SetAlpha32 は PixelFormat.Format32bppArgb の元画像のアルファ値を
均一に設定した値に減少させる。




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using ImageUtils;

namespace LuminanceToAlpha
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static bool LuminanceToAlpha(Bitmap bmp, bool bStretch,
            Color baseColor, out Bitmap result32)
        {
            result32 = null;

            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            Bitmap bw = bmp.Clone() as Bitmap;
            ImgUtils.GrayScale(ref bw);
            if (bStretch) ImgUtils.HistoStretch(ref bw);

            result32 = new Bitmap(w, h, PixelFormat.Format32bppArgb);

            Graphics g = Graphics.FromImage(result32);
            g.Clear(baseColor);
            g.Dispose();

            BmpProc8 src = new BmpProc8(bw);
            BmpProc32 dst = new BmpProc32(result32);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    dst[x, y, eRGB.a] = (byte)(255 - src[x, y]);
                }
            ImgUtils.CallDispose(dst, src, bw);

            return true;
        }

        public static bool SetAlpha32(ref Bitmap bmp, int percent)
        {
            if (bmp.PixelFormat != PixelFormat.Format32bppArgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            double p = percent / 100d;

            BmpProc32 dst = new BmpProc32(bmp);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    if (dst[x, y, eRGB.a] == 0) continue;
                    dst[x, y, eRGB.a] = ImgUtils.AdjustByte(dst[x, y, eRGB.a] * p);
                }
            dst.Dispose();

            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(@"c:\Home\ImgWork\SASample2.png");

            int w = bmp.Width;
            int h = bmp.Height;

            Bitmap tmp1, tmp2;
            Color baseColor = Color.FromArgb(10, 00, 70);

            LuminanceToAlpha(bmp, true, baseColor, out tmp1);


            Graphics g = this.CreateGraphics();

            for (int i = 0; i < 5; i++)
            {
                tmp2 = tmp1.Clone() as Bitmap;
                SetAlpha32(ref tmp2, 15 + 20 * i);
                g.DrawImage(tmp2, 20 + 150 * i, 50 + 30 * i, w, h);
                tmp2.Dispose();
            }

            g.Dispose();

            tmp1.Dispose();
            bmp.Dispose();
        }
    }
}
Comment

電話ボックス

2008-03-22 00:11:58 | 写真


天久保1、つくば市




[Sigma 17-70mmF2.8-4.5DCMacro]
[HDR]



Comment

Y

2008-03-22 00:02:05 | rinkaku






Comment

LuminanceToAlpha

2008-03-21 23:11:44 | C#(Graphics)

旧ブログからのコード移植、その1。

LuminanceToAlpha は元画像の輝度の反転(255から引いたもの)した値に
比例して任意の色シートのアルファ値を設定する。

色つきのカーボンコピーをつくる感じ。





全コードをしめす。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using ImageUtils;

namespace LuminanceToAlpha
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static bool LuminanceToAlpha(Bitmap bmp, bool bStretch,
            Color baseColor, out Bitmap result32)
        {
            result32 = null;

            if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                return false;

            int w = bmp.Width;
            int h = bmp.Height;

            Bitmap bw = bmp.Clone() as Bitmap;
            ImgUtils.GrayScale(ref bw);
            if (bStretch) ImgUtils.HistoStretch(ref bw);

            result32 = new Bitmap(w, h, PixelFormat.Format32bppArgb);

            Graphics g = Graphics.FromImage(result32);
            g.Clear(baseColor);
            g.Dispose();

            BmpProc8 src = new BmpProc8(bw);
            BmpProc32 dst = new BmpProc32(result32);

            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    dst[x, y, eRGB.a] = (byte)(255 - src[x, y]);
                }
            ImgUtils.CallDispose(dst, src, bw);

            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(@"c:\Home\ImgWork\LASample.png");

            int w = bmp.Width;
            int h = bmp.Height;

            Bitmap tmp;
            Color baseColor;

            Graphics g = this.CreateGraphics();

                baseColor = Color.FromArgb(100, 0, 0);

                LuminanceToAlpha(bmp, true, baseColor, out tmp);            
                g.DrawImage(tmp, 20, 50, w, h);
                tmp.Dispose();


                baseColor = Color.FromArgb(0, 100, 0);

                LuminanceToAlpha(bmp, true, baseColor, out tmp);
                g.DrawImage(tmp, 20 + (w - 50), 50, w, h);
                tmp.Dispose();


                baseColor = Color.FromArgb(0, 0, 100);

                LuminanceToAlpha(bmp, true, baseColor, out tmp);
                g.DrawImage(tmp, 20 + (w - 50) * 2, 50, w, h);
                tmp.Dispose();

            g.Dispose();

            
            bmp.Dispose();
        }
    }
}

Comment

福寿草

2008-03-21 22:47:42 | 写真

福寿草のめしべはイソギンチャクに似ている。







茨城県フラワーパーク、石岡市





[DFA Macro 100mmF2.8]
一枚目は non-HDR
二枚目は [HDR]



Comment

N

2008-03-20 00:33:41 | rinkaku


Comments (2)

Dahlia #9

2008-03-20 00:24:40 | 写真


茨城県フラワーパーク、石岡市




[DFA Macro 100mmF2.8]
[HDR]




Comment

土浦港 #2

2008-03-20 00:19:24 | 写真







土浦港、土浦市






[Sigma 17-70mmF2.8-4.5DCMacro]
[HDR]




Comment

N

2008-03-20 00:01:47 | rinkaku


Comment