Context : The star \gamma Cep is known as a single-lined spectroscopic triple system at a distance of 13.8 pc , composed of a K1 III-IV primary star with V = 3.2 mag , a stellar-mass companion in a 66–67 year orbit ( Torres 2006 ) , and a substellar companion with M _ { p } \sin i = 1.7 M _ { Jup } that is most likely a planet ( Hatzes et al . 2003 ) . Aims : We aim to obtain a first direct detection of the stellar companion , to determine its current orbital position ( for comparison with the spectroscopic and astrometric data ) , its infrared magnitude and , hence , mass . Methods : We use the Adaptive Optics camera CIAO at the Japanese 8m telescope Subaru on Mauna Kea , Hawaii , with the semi-transparent coronograph to block most of the light from the bright primary \gamma Cep A , and to detect at the same time the faint companion B . In addition , we also used the IR camera \Omega Cass at the Calar Alto 3.5m telescope , Spain , to image \gamma Cep A and B by adding up many very short integrations ( without AO ) . Results : \gamma Cep B is clearly detected on our CIAO and \Omega Cass images . We use a photometric standard star to determine the magnitude of B after PSF subtraction in the Subaru image , and the magnitude difference between A and B in the Calar Alto images , and find an average value of K = 7.3 \pm 0.2 mag . The separations and position angles between A and B are measured on 15 July 2006 and 11 and 12 Sept 2006 , B is slightly south of west of A . Conclusions : By combining the radial velocity , astrometric , and imaging data , we have refined the binary orbit and determined the dynamical masses of the two stars in the \gamma Cep system , namely 1.40 \pm 0.12 M _ { \odot } for the primary and 0.409 \pm 0.018 M _ { \odot } for the secondary ( consistent with being a M4 dwarf ) . We also determine the minimum mass of the sub-stellar companion to be M _ { p } \sin i = 1.60 \pm 0.13 M _ { Jup } .